我正在使用rails 3.2.8,globalize3和batch_translations为一个小的cms和商店系统翻译特定的内容。我在一个模型上进行了一次翻译,没有任何问题。所以所有的东西都运行得很好。我开始为我的其他模型添加这个功能,and..shhhhh奇怪的事情发生了。
现在状态:我可以用翻译创建新的内容。一切都很好。但是,如果我试图编辑/更新翻译表中的值,什么也不会发生!也许在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我写这个奇怪的翻译类是因为我得到了大量分配给语言环境的错误,等等。
表格:
<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
endcategories_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有没有人有想法,或者有两个模型有一个或多个翻译属性的工作示例?
发布于 2012-10-18 22:25:42
我的错:
模型的更新:
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
endhttps://stackoverflow.com/questions/12953842
复制相似问题