2012-10-18 25 views
0

我正在使用rails 3.2.8,globalize3和batch_translations來翻譯cms和shop系統的特定內容。 我在一個模型上整合了一個翻譯,沒有問題。所以everthings工作正常。我開始爲我的其他模型添加這個功能,並且..shhhhh奇怪的事情發生。Rails:Globalize3和batch_translations

現狀:我可以用翻譯創建新內容。一切還好。但是,如果我嘗試編輯/更新翻譯表中的值,則不會發生任何事情!也許在batch_translations中有錯誤的參數路徑或其他東西...

這裏是一個類別的例子!

migration_file

class CreateCategories < ActiveRecord::Migration 
    def self.up 
    create_table :categories do |t| 
     t.timestamps 
    end 
    Category.create_translation_table! :category_name => :string, :description => :string 
    end 

    def self.down 
    Category.drop_translation_table! 
    drop_table :categories 
    end 
end 

型號:

class Category < ActiveRecord::Base 
    attr_accessible :category_name, :description 

    attr_accessible :translations_attributes 
    translates :category_name, :description 
    has_many :translations 
    accepts_nested_attributes_for :translations 

    class Translation 
    attr_accessible :locale, :category_name, :description 
    end 
end 

這種怪異的類翻譯我寫的,因爲我得到了現場質量assignemnt錯誤等等......

形式:

<div> 
    <%= form_for @category, html: { :multipart => true } do |f| %> 
    <%= render 'shared/error_messages', object: f.object %> 
    <%= build_translation_text_field f, :category_name, :description %> 
    <%= f.submit (t ".save"), class: "btn btn-large btn-primary" %> 
    <% end %> 
</div> 

幫手我的翻譯形式:

def build_translation_text_field(f, *atts) 
    tag = content_tag(:h1, "Test") 
    I18n.available_locales.each do |l| 
    f.globalize_fields_for l do |g| 
     atts.each do |a| 
     tag += content_tag(:div, content_tag(:h4, t(a)+":")) 
     tag += (g.text_field a, class: l) 
     end 
    end 
    end 
    tag 
end 

categories_controller更新方法:

def create 
    @category = Category.new(params[:category]) 
    if @category.save 
    @categories = Category.all 
    flash[:success] = t(:category_created) 
    respond_to do |format| 
     format.html {render 'index'} 
     format.js 
    end 
    else 
    flash[:error] = @category.errors.full_messages.each {|msg| p msg} 
    @categories = Category.all 
    respond_to do |format| 
     format.html {render 'new'} 
     format.js 
    end 
    end 
end 

def update 
    @category = Category.find(params[:id]) 
    if @category.update_attributes(params[:category]) 
    @categories = Category.all 
    flash[:success] = t(:category_updated) 
    respond_to do |format| 
     format.html {render 'index'} 
     format.js 
    end 
    else 
    flash[:error] = @category.errors.full_messages.each {|msg| p msg} 
    @categories = Category.all 
    respond_to do |format| 
     format.html {render 'edit'} 
     format.js 
    end 
    end 
end 

任何想法或有兩種型號白衣的一個或多個翻譯屬性的工作的例子嗎?

回答

0

我的錯:

更新模型:

class Category < ActiveRecord::Base 
    attr_accessible :category_name, :description 

    attr_accessible :translations_attributes 
    translates :category_name, :description 
    # has_many :translations <-- delete this 
    accepts_nested_attributes_for :translations 

    class Translation 
    attr_accessible :locale, :category_name, :description 
    end 
end