Rails4嵌套表单不保存

时间:2014-08-05 17:36:18

标签: ruby-on-rails ruby ruby-on-rails-4 nested-forms

我有一个三重嵌套表单,其中表单有很多问题和问题有很多答案。我在展示页面上有每个表单渲染,其中包含表单标题和表单问题以及输入字段,以创建与问题相关的新答案。该关联很好地连接,但我似乎无法得到插入数据库的答案。我觉得这很容易,但我似乎无法弄明白。

- 形式 -

<%= simple_form_for @form do |f|  %>
  <%= f.error_notification  %>
  <h1><%= @form.name  %></h1>
  <div class="fields">
    <%= f.simple_fields_for :form_questions do |q| %>
      <%= label_tag q.index+1 %>
      <%= q.simple_fields_for :form_answers, q.object.form_answers.build do |a|  %>
        <%= a.input :answer, label: false %>
      <% end %>
    <% end %>
  </div>
  <div class="fields">
    <%= f.button :submit, :class => "button", value: 'Submit'  %>
  </div>
<% end %>

- 形式控制器 -

class FormsController < ApplicationController

    def new
      @form = Form.new
      @form.form_questions.form_answers.build
    end

    def create
      @form = Form.new(form_params)
    end

    def index
      @forms = Form.includes(:form_questions).all
    end

    def show
      @form = Form.find(params[:id])
    end

    def edit
      @form = Form.find(params[:id])
    end


    def form_params
      params.require(:form).permit(:id, :name, form_questions_attributes: [:title, form_answers_attributes: [:answer]])
    end

  end

-Form Answer Controller(虽然如果关联正确,我觉得我应该能够处理表单控制器中的所有内容)

class FormAnswersController < ApplicationController

  def new
    @form_answer = FormAnswer.new
  end

  def create
    @form_answer = FormAnswer.new(form_answer_params)
    if @form_answer.save
      RequestMailer.request_submit(@form).deliver
    else
      format.html {render action: 'new'}
    end
  end

  def form_answers_params
    params.require(:form_answer).permit(:form_id, :form_question_id, :id, :answer)
  end

end

-Server Logs -

Started PATCH "/forms/12" for 127.0.0.1 at 2014-08-05 13:31:44 -0400
Processing by FormsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"CcaCGjdoYPH2Boi1prIFhG+h5YuPHQvAxMp1dcKvWr8=", "form"=>{"form_questions_attributes"=>{"0"=>{"form_answers_attributes"=>{"0"=>{"answer"=>"hey"}}, "id"=>"50"}, "1"=>{"form_answers_attributes"=>{"0"=>{"answer"=>"ya"}}, "id"=>"51"}, "2"=>{"form_answers_attributes"=>{"0"=>{"answer"=>"go"}}, "id"=>"52"}}}, "commit"=>"Submit", "id"=>"12"}
  Rendered forms/update.html.erb within layouts/application (0.1ms)
Completed 200 OK in 12ms (Views: 11.4ms | ActiveRecord: 0.0ms)

0 个答案:

没有答案