Rails 4-使用accepts_nested_attributes_for控制器设置的嵌套表单? [英] Rails 4 - Nested form with accepts_nested_attributes_for controller setup?

查看:69
本文介绍了Rails 4-使用accepts_nested_attributes_for控制器设置的嵌套表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用form_forfields_for制作嵌套表格.经过大量研究和成功,不再从事我的项目.我只是想重新创建轨道广播,以查看我做错了什么.

I'm trying to make a nested form with form_for and fields_for. After much research and in-success, not working on my project anymore. I'm just trying to recreate a railscast to see what have I done wrong.

我正在尝试重新创建在 http:中找到的示例: //railscasts.com/episodes/196-nested-model-form-part-1 ,因为其中已有代码,所以它并不难,但我无法从调查中提出问题.到目前为止,这是我的代码:

I'm trying to re-create the example found at http://railscasts.com/episodes/196-nested-model-form-part-1 which shouldn't be that hard since the code is there, but I can't manage to create questions from the survey. Here is my code until now:

rails new surveysays
rails g scaffold survey name:string
rake db:migrate
rails g scaffold question survey_id:integer content:text
rake db:migrate

我正在尝试按照与视频完全相同的顺序进行操作. 我的问题模型:

I'm trying to do in the exact same sequence of the video. My Question model:

class Question < ActiveRecord::Base
  belongs_to :survey
end

我的调查模型:

class Survey < ActiveRecord::Base
  has_many :questions, dependent: :destroy
  accepts_nested_attributes_for :questions
end

我的调查表带有嵌套的问题字段:

My survey form with nested questions fields:

<%= form_for(@survey) do |f| %>
      ...
      <div class="field">
        <%= f.label :name %><br>
        <%= f.text_field :name %>
      </div>
      <%= f.fields_for :questions do |builder| %>
        <p>
          <%= builder.label :content, "Question" %><br/>
          <%= builder.text_area :content, :row => 3 %>
        </p>
      <% end %>
      <div class="actions">
        <%= f.submit %>
      </div>
   <% end %>

我的调查显示:

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @survey.name %>
</p>

<ol>
  <% for question in @survey.questions %>
   <li><%=h question.content%></li>
  <% end %>
</ol>

<%= link_to 'Edit', edit_survey_path(@survey) %> |
<%= link_to 'Back', surveys_path %>

还有我的SurveysController:

And my SurveysController:

class SurveysController < ApplicationController
 ...

  # GET /surveys/new
  def new
    @survey = Survey.new
    3.times { @survey.questions.build }
  end

 ...

  # POST /surveys
  # POST /surveys.json
  def create
    @survey = Survey.new(survey_params)

    respond_to do |format|
      if @survey.save
        format.html { redirect_to @survey, notice: 'Survey was successfully created.' }
        format.json { render :show, status: :created, location: @survey }
      else
        format.html { render :new }
        format.json { render json: @survey.errors, status: :unprocessable_entity }
      end
    end
  end

  ...

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_survey
       @survey = Survey.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def survey_params
       params.require(:survey).permit(:name)
    end
end

直到凌晨5:34为止,这就是视频中不起作用且未创建问题的情况,该表单出现了3个问题,我填写了表单,但是按 create 不会创建问题:

As until min 5:34 and thats when it doesn't work as shown in the video and doesn't create the questions, the form appears with the 3 questions, I fill the form, but when a press create it doesn't create the questions:

加载开发环境(Rails 4.1.6) 2.1.3:001> s = Survey.all 调查负荷(3.0毫秒)选择调查".*从调查" =>#]> 2.1.3:002> q = s [0].问题 问题负载(0.6毫秒)在问题"中选择问题".*从问题"中选择."survey_id" =? [["survey_id",2]] =>#

Loading development environment (Rails 4.1.6) 2.1.3 :001 > s = Survey.all Survey Load (3.0ms) SELECT "surveys".* FROM "surveys" => #]> 2.1.3 :002 > q = s[0].questions Question Load (0.6ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ? [["survey_id", 2]] => #

我的代码和示例之间看不到任何区别.我什至尝试在SurveysController中进行一些更改,但均未成功:

I can't see any difference between my code and the example. I even tried to make some alterations in the SurveysController without any success:

在方法survey_params的允许下插入question_attributes:[:id,:content] 或者 如果Survey.save保留在create方法之后,则插入@ survey.questions.create(survey_params [:questions_attributes]),这将创建问题,但内容为:nill

Inserting question_attributes:[:id ,:content] in permit of method survey_params or Inserting @survey.questions.create(survey_params[:questions_attributes]) after if survey.save on create method, this creates the question but with content: nill

在这一点上,我被困住了.我不知道该怎么办,我还缺少控制器吗? 谁能给我一些帮助,谢谢.

At this point I am stuck. I don't know what more what to do, what am I missing int the controller? Can anyone give me some help, thanks.

推荐答案

在控制器中的survey_params方法上,您缺少问题参数,它看起来应该像:

On the survey_params method in the controller, you are missing the question params, it should look like:

def survey_params
 params.require(:survey).permit(:name, :questions_attributes => [:question, :answer ... or whatever attribute for the question model])
end

让我知道怎么回事!

这篇关于Rails 4-使用accepts_nested_attributes_for控制器设置的嵌套表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆