如何解决“无法批量分配受保护的属性:translations_attributes"错误? [英] How to solve the "Can't mass-assign protected attributes: translations_attributes" error?
问题描述
我正在使用Ruby on Rails(3.2.2), globalize3 (0.2.0)和 batch_translations (0.1.2)ruby-gems.我想解决使用batch_translations ruby-gem时产生的以下问题:
I am using Ruby on Rails (3.2.2), globalize3 (0.2.0) and batch_translations (0.1.2) ruby-gems. I would like to solve the following problem generated when using the 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
文件中使用以下代码找到了解决方案:
I found the solution by using the following code in the ROOT_RAILS/app/models/admin/article.rb
file:
class Admin::Article < ActiveRecord::Base
translates :title
attr_accessible :translations_attributes
accepts_nested_attributes_for :translations
...
end
...但是确定可以访问:translations_attributes
吗?
... but making the :translations_attributes
accessible is it sure?
推荐答案
自最新版本的rails对其进行修补以来,这将是一个问题.您可以在配置中更改它.参见 http://weblog .rubyonrails.org/2012/3/30/ann-rails-3-2-3-has-been-released/了解详情.
This will be an issue with the newest version of rails since they patched it. You can change it in the config. See http://weblog.rubyonrails.org/2012/3/30/ann-rails-3-2-3-has-been-released/ for details.
我可以确认您的attr_accessible解决方案是正确的.
I can confirm that your attr_accessible solution is correct.
这篇关于如何解决“无法批量分配受保护的属性:translations_attributes"错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!