ruby - How do i work with nested model attributes in refinerycms? -
i have seen http://railscasts.com/episodes/196-nested-model-form-revised , tried same doing refinery engine, not able see nested fields in form while adding record admin section , not getting errors. unable figure out happening or may if missing refinery related configuration thing.
i tried on rails console:
refinery::extension::model.nested_attributes_options => {:nested_model_name=>{:allow_destroy=>false, :update_only=>false}}
i have 2 models question , option, while submitting form question options nested elements giving me error 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 , 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 nested fields :
<%= f.fields_for :refinery_papers_options |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 tried on rails console got 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 /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' /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' /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' /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activerecord-3.2.14/lib/active_record/base.rb:498:in
initialize' /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activerecord-3.2.14/lib/active_record/persistence.rb:44:innew' /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/activerecord-3.2.14/lib/active_record/persistence.rb:44:in
create' (irb):7 /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/railties-3.2.14/lib/rails/commands/console.rb:47:instart' /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/railties-3.2.14/lib/rails/commands/console.rb:8:in
start' /home/vivek/.rvm/gems/ruby-2.0.0-p247@refinery/gems/railties-3.2.14/lib/rails/commands.rb:41:in<top (required)>' script/rails:6:in
require' script/rails:6:in `'
may 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 confusion regarding class names
of different models have created in engine. above code piece of example how refinery core
team have done nested attributes concept
.
Comments
Post a Comment