为什么嵌套的cocoon表单没有保存数据?

时间:2014-03-25 03:14:56

标签: ruby-on-rails cocoon-gem

我是ruby on rails的新手,我拼命想让嵌套表单真正起作用。所以我创建了一个简单的小应用程序,试图让它使用cocoon。

这个想法是:

  1. 创建一个始终分配给“section”的嵌套“问题”
  2. 创建“部分”时,应该能够添加嵌套的“问题”并与部分相关联
  3. 目前我能够看到问题的字段,输入我的数据,但是当我点击“提交”时。问题不会保存到数据库中。我不知道为什么,我不确定如何添加它们,

    所以...这里有一些代码可以仔细阅读:

    我的部分表格部分:

    %h2 Sections
    = semantic_form_for @section do |f|
      = f.inputs do
        = f.input :title
    
        %hr
        - @section.printQuestions @section
        %hr
    
        %h3 Questions
        #questions
        = f.semantic_fields_for :questions do |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'}
    

    - 单击提交时,将保存该部分,但不保存新制作的问题

    部分目录中找到的“问题”部分:

    .question_fields
      = f.inputs do
        %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
    

    模型权限:

    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]
        ## the rest is 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
    
    代码运行时

    输出:

    Started PATCH "/sections/5" for 127.0.0.1 at 2014-03-25 00:01:24 -0400
    Processing by SectionsController#update as 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".* FROM "sections" WHERE "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 to http:5
    Completed 302 Found in 46ms (ActiveRecord: 1.0ms)
    

0 个答案:

没有答案