私はRubyonRails(3.2.2)、globalize3(0.2.0)、batch_translations(0.1.2)ruby-gemsを使用しています。batch_translationsruby-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
アクセス可能にすることは確かですか?