How can I work with nested model attributes in refinerycms?

I saw http://railscasts.com/episodes/196-nested-model-form-revised and tried to do the same with my processing engine, but I can’t see the nested fields in my form when adding an entry from the admin section, and also i don't get any errors. I can’t understand what exactly is happening or can happen if I don’t have any configuration related to the refinery.

I tried this on the Rails console:

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

I have 2 Question and Option models, but when I submit a form for a question with parameters as nested elements, it gives me an error, as shown below

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

Cannot assign protected attributes: refinery_papers_options

Request

Parameters:

{"utf8" => "βœ“", "Authenticity_token" => "TQL + r60R05 + meVhPBXPPipvL + X3ZNx + 3dCwoThFBn / Y =", "question" => {"content" => "

aaaaaaaaaaa

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

asdfghjklkmnv

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

My models and view:

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, the Form for nested fields looks 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 the 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: Cannot 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 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 / r uby-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 start '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:in require' from script / rails: 6: in ``
+4
source share
1 answer

Perhaps this will 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 various models that you created in the engine. This code above is just an example of how the refinery core team executed the nested attributes concept .

0
source

Source: https://habr.com/ru/post/1500489/


All Articles