gpt4 book ai didi

ruby-on-rails-4 - Rails 4 - 带有 accepts_nested_attributes_for Controller 设置的嵌套表单?

转载 作者:行者123 更新时间:2023-12-04 22:04:24 25 4
gpt4 key购买 nike

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

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

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

我正在尝试按照视频的完全相同的顺序进行操作。
我的问题模型:
class Question < ActiveRecord::Base
belongs_to :survey
end

我的调查模型:
class Survey < ActiveRecord::Base
has_many :questions, dependent: :destroy
accepts_nested_attributes_for :questions
end

我的带有嵌套问题字段的调查表:
<%= 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:
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 个问题,我填写了表单,但是当按下创建时,它不会创建问题:

加载开发环境(Rails 4.1.6)
2.1.3 :001 > s = Survey.all
调查负载 (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 进行一些更改,但没有成功:

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

在这一点上,我被卡住了。我不知道还能做什么,我在 Controller 中缺少什么?
谁能给我一些帮助,谢谢。

最佳答案

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

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

让我知道事情的后续!

关于ruby-on-rails-4 - Rails 4 - 带有 accepts_nested_attributes_for Controller 设置的嵌套表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27682951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com