4

我已经看到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: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:in assign_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:in start' 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开始:在`'

4

1 回答 1

0

可能这个链接可以提供帮助。

 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.

于 2013-09-04T11:14:34.813 回答