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
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 ``