我已经看过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}}
我有两个模型问题和选项,但在提交问题表单时,选项作为嵌套元素,它给我如下错误
ActiveModel::MassAssignmentSecurity::Error in Refinery::Papers::Admin::QuestionsController#create
无法批量指定受保护的属性:refinery_papers_options
请求
参数:
{ “UTF8”=> “中✓”, “authenticity_token”=> “中TQL + r60R05 + meVhPBXPPipvL + X3ZNx + 3dCwoThFBn / Y =”, “问题”=> {“content”=>“
aaaaaaaaaa
”,“correct_answers”=>“a”, “refinery_papers_options”=> { “内容”=> “中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&gt;炼油厂::论文:: Question.create({ “内容”=&gt; “中
jhsdacnlkS
”, “correct_answers”=&gt; “中一个”, :refinery_papers_options =&gt; { “内容”=&gt; “中sjdfgczdj
”}}) ActiveModel :: MassAssignmentSecurity ::错误:无法进行质量分配 受保护的属性:来自的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:in 来自process_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:in 来自sanitize' 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:innew' from /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activerecord-3.2.14/lib/active_record/persistence.rb:44:in
创建'from(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:在`'
答案 0 :(得分:0)
这可能是link 可以帮助。
has_many :parts,
:foreign_key => :refinery_page_id,
:class_name => '::Refinery::PagePart',
:order => 'position ASC',
:inverse_of => :page,
:dependent => :destroy,
:include => ((:translations) if ::Refinery::PagePart.respond_to?(:translation_class))
accepts_nested_attributes_for :parts, :allow_destroy => true
您在引擎中创建的不同模型的class names
可能存在混淆。上面的代码只是refinery core
小组如何完成nested attributes concept
的一个示例。