我正在使用 Ruby on Rails (3.2.2)、globalize3 (0.2.0) 和batch_translations (0.1.2) ruby-gems。我想解决使用 batch_translations ruby-gem 时产生的以下问题:
ActiveModel::MassAssignmentSecurity::Error in Admin::ArticlesController#update
Can't mass-assign protected attributes: translations_attributes
在我的ROOT_RAILS/Gemfile
文件中,我有:
...
gem 'globalize3'
gem 'batch_translations'
在我的ROOT_RAILS/app/models/admin/article.rb
文件中,我有:
class Admin::Article < ActiveRecord::Base
translates :title
# This is needed to make the batch_translations to work.
accepts_nested_attributes_for :translations
...
end
在我的ROOT_RAILS/app/views/admin/articles/_form.html.erb
文件中,我有:
<%= form_for(@admin_article, :url => admin_article_path) do |f| %>
<%= f.label :title %><br />
English translation:
<%= f.text_field :title %>
Italiano translation:
<%
# Note: I am using the '<%= f...' instad of '<% f...' otherwise
# batch_translations doesn't output the input field in the
# front-end content.
%>
<%= f.globalize_fields_for :it do |g| %>
<%= g.text_field :title %>
<% end %>
<% end %>
在我的ROOT_RAILS/app/controllers/admin/articles_controller.html.erb
文件中,我有:
class Admin::ArticlesController < ApplicationController
def update
@admin_article = Article.find(params[:id])
respond_to do |format|
if @admin_article.update_attributes(params[:article])
format.html { redirect_to admin_article_path(@admin_erticle), notice: 'Article was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @admin_article.errors, status: :unprocessable_entity }
end
end
end
...
end
当我显示编辑表单所有工作时,但是当我提交该表单时,我得到了上面提到的错误。 如何解决上述错误?
更新
我通过在文件中使用以下代码找到了解决方案ROOT_RAILS/app/models/admin/article.rb
:
class Admin::Article < ActiveRecord::Base
translates :title
attr_accessible :translations_attributes
accepts_nested_attributes_for :translations
...
end
...但是:translations_attributes
确定可访问性吗?