4
votes

I have seen http://railscasts.com/episodes/196-nested-model-form-revised and tried the same doing with my refinery engine, but i am not able to see the nested fields in my form while adding a record from admin section and also I am not getting any errors. I am unable to figure out what exactly is happening or may be if i am missing any refinery related configuration thing.

I tried this on Rails Console:

 Refinery::Extension::Model.nested_attributes_options
 => {:nested_model_name=>{:allow_destroy=>false, :update_only=>false}}

I have 2 models Question and Option, but while submitting form for question with options as nested elements it is giving me Error as below

  ActiveModel::MassAssignmentSecurity::Error in  Refinery::Papers::Admin::QuestionsController#create

Can't mass-assign protected attributes: refinery_papers_options

Request

Parameters:

{"utf8"=>"✓", "authenticity_token"=>"TqL+r60R05+meVhPBXPPipvL+X3ZNx+3dCwoThFBn/Y=", "question"=>{"content"=>"

aaaaaaaaaa

", "correct_answers"=>"a", "refinery_papers_options"=>{"content"=>"

asdfghjklkmnv

", "_destroy"=>"0"}, "position"=>0}, "locale"=>:en}

My Models and view Are:

Question Model:

    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

In views Form for nested fields is like this :

<%= 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 %>

When i tried this on rails console i got this stack

2.0.0p247 :007 > Refinery::Papers::Question.create({"content"=>"

jhsdacnlkS

","correct_answers"=>"a", :refinery_papers_options => {"content"=>"

sjdfgczdj

"}}) ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: refinery_papers_options from /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' from /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' from /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 initialize' from /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 create' from (irb):7 from /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:instart' from /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:inrequire' from script/rails:6:in `'
1
what errors did you get?Rajarshi Das
i dint got any error.It was working as fine and same as it was before but was not ale to see the nested attributes.vivekporwal04
@RajarshiDas hey now i am able to see the fields in views but after submitting the form its giving me mass assignment error for options can help with that.vivekporwal04
sorry for late reply .....if you use rails 4 use strong_parameter or if rails 3.2 use attr_accessible :name #all fields in your model which are assigned by also another solution config.active_record.whitelist_attributes = true see the link whitelistRajarshi Das
@RajarshiDas thanks for reply,i have already done what you are advising, you can check the models code and views code which i have posted above,but instead of that i am getting the mass assignment problem.vivekporwal04

1 Answers

0
votes

May be this link can help.

 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

There may be confusion regarding the class names of different models you have created in the engine. This above code is just a piece of example how the refinery core team have done the nested attributes concept.