我已经看到http://railscasts.com/episodes/196-nested-model-form-revised并尝试对我的炼油厂引擎做同样的事情,但是我在添加记录时无法看到表单中的嵌套字段管理部分,我也没有收到任何错误。我无法弄清楚到底发生了什么,或者如果我遗漏了任何与炼油厂相关的配置内容,可能会发生什么。
我在 Rails 控制台上试过这个:
Refinery::Extension::Model.nested_attributes_options
=> {:nested_model_name=>{:allow_destroy=>false, :update_only=>false}}
我有 2 个模型问题和选项,但是在提交带有选项作为嵌套元素的问题表单时,它给了我如下错误
ActiveModel::MassAssignmentSecurity::Error in Refinery::Papers::Admin::QuestionsController#create
无法批量分配受保护的属性:refinery_papers_options
要求
参数:
{"utf8"=>"✓", "authenticity_token"=>"TqL+r60R05+meVhPBXPPipvL+X3ZNx+3dCwoThFBn/Y=", "问题"=>{"内容"=>"
啊啊啊啊
", "correct_answers"=>"a", "refinery_papers_options"=>{"content"=>"asdfghjklkmnv
", "_destroy"=>"0"}, "position"=>0}, "locale"=>:en}
我的模型和视图是:
问题模型:
module Refinery
module Papers
class Question < Refinery::Core::BaseModel
self.table_name = 'refinery_papers_questions'
attr_accessible :content, :correct_answers, :options_attributes, :position
validates :content, :presence => true, :uniqueness => true
has_many :options,
:foreign_key => "refinery_papers_question_id",
:class_name => "Refinery::Papers::Option",
:dependent => :destroy
accepts_nested_attributes_for :options,
:allow_destroy => true
end
end
end
Option Model:
module Refinery
module Papers
class Option < Refinery::Core::BaseModel
self.table_name = 'refinery_papers_options'
attr_accessible :content, :position, :refinery_papers_question_id
validates :content, :presence => true
belongs_to :question,
:class_name => 'Refinery::Papers::Question',
:foreign_key => :refinery_papers_question_id
end
end
end
在视图中嵌套字段的表单是这样的:
<%= f.fields_for :refinery_papers_options do |option_form| %>
<div class='field'>
<%= option_form.label :content, "Option" %><br/>
<%= option_form.text_area :content, :class => "wymeditor widest" %><br/>
</div>
<div class='field'>
<%= option_form.label :_destroy, "Remove Option" -%>
<%= option_form.check_box :_destroy -%>
</div>
<% end %>
当我在 Rails 控制台上尝试这个时,我得到了这个堆栈
2.0.0p247 :007 > 炼油厂::论文::Question.create({"content"=>"
jhsdacnlkS
","correct_answers"=>"a", :refinery_papers_options => {"content"=>"sjdfgczdj
"}}) ActiveModel::MassAssignmentSecurity::Error: 无法批量分配受保护的属性:refinery_papers_options 来自 /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activemodel-3.2.14 /lib/active_model/mass_assignment_security/sanitizer.rb:48:inprocess_removed_attributes' from /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activemodel-3.2.14/lib/active_model/mass_assignment_security/sanitizer.rb:20:in
debug_protected_attribute_removal' 来自 /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activemodel-3.2.14/lib/active_model/mass_assignment_security /sanitizer.rb:12:insanitize' from /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activemodel-3.2.14/lib/active_model/mass_assignment_security.rb:230:in
sanitize_for_mass_assignment' 来自 /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activerecord-3.2.14/lib/active_record/attribute_assignment.rb:75:inassign_attributes' from /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activerecord-3.2.14/lib/active_record/base.rb:498:in
从 /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activerecord-3.2.14/lib/active_record/persistence.rb:44:in 初始化new' from /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activerecord-3.2.14/lib/active_record/persistence.rb:44:in
从(irb)创建':7来自/home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/railties-3.2.14/lib/rails/commands/console.rb:47:instart' from /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in
从 /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/railties-3.2.14/lib/rails/commands.rb:41:in<top (required)>' from script/rails:6:in
要求'从 script/rails:6开始:在`'