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

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

问题描述

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

我正在尝试重新创建在 http 中找到的示例://railscasts.com/episodes/196-nested-model-form-part-1 这应该不难,因为代码在那里,但我无法从调查中创建问题.这是我到现在为止的代码:

rails 新的调查说rails g 脚手架调查名称:字符串耙数据库:迁移rails g 脚手架问题survey_id:integer content:text耙数据库:迁移

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

类问题

我的调查模型:

班级调查

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

<%= form_for(@survey) do |f|%>...<div class="field"><%= f.label :name %><br><%= f.text_field :name %>

<%= f.fields_for :questions do |builder|%><p><%= builder.label :content, "问题" %><br/><%= builder.text_area :content, :row =>3%></p><%结束%><div class="actions"><%= f.submit %>

<%结束%>

我的调查显示:

<%= notice %>

<p><strong>名称:</strong><%=@survey.name %></p><ol><@survey.questions 中的问题%><li><%=h question.content%</li><%结束%></ol><%= link_to '编辑', edit_survey_path(@survey) %>|<%= link_to '返回',surveys_path %>

还有我的 SurveysController:

class SurveysController <应用控制器...# 获取/调查/新定义新@survey = Survey.new3.times { @survey.questions.build }结尾...# 发布/调查# POST/surveys.json定义创建@survey = Survey.new(survey_params)response_to do |格式|如果@survey.saveformat.html { redirect_to @survey,注意:'调查已成功创建.'}format.json { 渲染:显示,状态::创建,位置:@survey }别的format.html { 渲染:新}format.json { 渲染 json:@survey.errors,状态::unprocessable_entity }结尾结尾结尾...私人的# 使用回调在动作之间共享公共设置或约束.def set_survey@survey = Survey.find(params[:id])结尾# 永远不要相信来自可怕互联网的参数,只允许白名单通过.定义调查参数params.require(:survey).permit(:name)结尾结尾

直到 5 点 34 分,当视频中显示它不起作用并且不创建问题时,表格出现了 3 个问题,我填写了表格,但是当按下 create 它不会创建问题:

加载开发环境(Rails 4.1.6)2.1.3 :001 > s = Survey.all调查负载 (3.0ms) SELECT "surveys".* FROM "surveys"=> #]>2.1.3 :002 > q = s[0].questionsQuestion Load (0.6ms) SELECT "questions".* FROM "questions" WHERE "questions"."survey_id" = ?[["survey_id", 2]]=> #

我看不出我的代码和示例之间有什么区别.我什至尝试在 SurveysController 中进行一些更改,但没有成功:

在方法调查参数的许可中插入 question_attributes:[:id ,:content]或者在创建方法的 ifsurvey.save 之后插入 @survey.questions.create(survey_params[:questions_attributes]),这会创建问题,但内容为:nill

此时我被卡住了.我不知道还能做什么,我在控制器中缺少什么?谁能给我一些帮助,谢谢.

解决方案

在控制器中的 survey_params 方法上,您缺少问题参数,它应该如下所示:

defsurvey_paramsparams.require(:survey).permit(:name, :questions_attributes => [:question, :answer ...或问题模型的任何属性])结尾

告诉我进展如何!

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.

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

My Survey model:

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

My survey show:

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

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

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:

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]] => #

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:

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.

解决方案

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

Let me know how it goes!

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

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