ruby on rails - Why does my nested cocoon form not save the data? -
i'm new ruby on rails , desperately trying nested forms work. have created simple little app try make work using cocoon.
the idea to:
- create nested "question" assigned "section"
- when creating "section", nested "questions" should able added , associated section
currently able see fields question, enter in data, when click "submit". questions not saved database. not sure why, , not sure how add them,
so... here's code peruse:
my section form partial:
%h2 sections = semantic_form_for @section |f| = f.inputs = f.input :title %hr - @section.printquestions @section %hr %h3 questions #questions = f.semantic_fields_for :questions |question| = render 'question_fields', :f => question .links = link_to_add_association 'add question', f, :questions, :data => {"association-insertion-method" => "after" } %p %input{type: 'submit', value: 'submit'} -- when submit clicked, section saved newly made questions not saved
the partial "questions" found within directory sections:
.question_fields = f.inputs %p = f.label :question_text %br = f.text_area :question_text, :rows => 2, :cols => 40 %p = f.label :answer_text %br = f.text_area :answer_text, :rows => 3, :cols => 60 = f.input :section_id, input_html: { readonly: true } = link_to_remove_association "remove question", f model permissions:
class section < activerecord::base has_many :questions accepts_nested_attributes_for :questions end class question < activerecord::base belongs_to :section end class questionscontroller < applicationcontroller before_action :set_question, only: [:show, :edit, :update, :destroy] ## rest auto-generated end class sectionscontroller < applicationcontroller before_action :set_section, only: [:show, :edit, :update, :destroy] def section_params params.require(:section).permit(:id, :title, :body, :good_opinion, :image_file_name, :questions_attributes [:id, :question_text, :answer_text, :section_id, :_destroy]) accepts_nested_attributes_for :questions end end output when code run:
started patch "/sections/5" 127.0.0.1 @ 2014-03-25 00:01:24 -0400 processing sectionscontroller#update html parameters: {"utf8"=>"√", "authenticity_token"=>"x6rdcamjurdlciewpotwskvqnrvvkgjv6fuzywmjrsy=", "section"=>{"id"=>"5", "title"=>"question text", "i mage_file_name"=>"image.png", "good_opinion"=>"0", "body"=>"body text", "questions_attributes"=>{"1395720076660"=>{"question_text"=>"qqqqqqqqqqqqqq", "answer_text"=>"aaaaaaaaaaaaaa", "section_id"=>"5", "_destroy"=>"false"}}}, "id"=>"5"} ←[1m←[35msection load (1.0ms)←[0m select "sections".* "sections" "sections"."id" = ? limit 1 [["id", "5"]] unpermitted parameters: id, questions_attributes ←[1m←[36m (0.0ms)←[0m ←[1mbegin transaction←[0m ←[1m←[35m (0.0ms)←[0m commit transaction redirected http:5 completed 302 found in 46ms (activerecord: 1.0ms)
Comments
Post a Comment