本文介绍了Rails 4 嵌套表单.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在为客户构建动态表单.一个表格有许多表格问题,而表格问题有许多表格答案.到目前为止,我能够在 Active Admin 中很好地创建所有内容,并通过应用程序界面上的显示操作显示它.这是我遇到的问题.我想显示表单标题(正在工作),以及表单问题(正在工作),以及用于即时提交新表单答案的输入字段(这是不工作的部分).当涉及到嵌套表单时,我觉得我已经用尽了一切.我将在下面发布我的代码.
表格
<%= f.fields_for :form_questions do |ff|%><div class="field"><%=ff.label:title%><%= ff.text_field :form_answers %>
<%结束%><div class="form-actions"><%= f.button :submit %>
<%结束%>
这是模型
class 表单 <ActiveRecord::Basehas_many :form_questions, 依赖: :destroyaccepts_nested_attributes_for :form_questions, allow_destroy: true结尾
class FormQuestion
class FormAnswer
还有我的表单控制器
class FormsController
解决方案
首先,您应该取消注释
new
方法中的这两行.我想它们是正确的.
def new@form = Form.new@form_questions = @form.form_questions.build@form_answers = @form_questions.form_answers.build结尾
并且在您的 create
操作中,您不会保存
data
def 创建@form = Form.new(form_params)如果@form.save.....别的.....结尾结尾
其次,你的form
代码应该是这样的
<%= f.fields_for @form_questions 做 |ff|%><div class="field"><%=ff.label:title%>
<%= ff.fields_for @form_answers 做 |fa|%>#重要的一步来了<div class="field" %><%= fa.label :answer %><%= fa.text_field :answer %>
<%结束%><%结束%><div class="form-actions"><%= f.button :submit %>
<%结束%>
I am building a dynamic form for a client. A form has many form questions which has many form answers. As of now, I am able to create everything nicely in Active Admin and have it displaying through the show action on the app interface. Here is the problem I have. I want to display the form title (which is working), along with the form questions (which is working), along with input fields to submit new form answers on the fly (which is the part that is not working). I feel like I have exhausted everything when it comes to nested forms. I will post my code below.
Form
<%= form_for @form do |f| %>
<div class="field">
<h1><%= @form.name %></h1>
</div>
<%= f.fields_for :form_questions do |ff| %>
<div class="field">
<%= ff.label :title %>
<%= ff.text_field :form_answers %>
</div>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Here is the models
class Form < ActiveRecord::Base
has_many :form_questions, dependent: :destroy
accepts_nested_attributes_for :form_questions, allow_destroy: true
end
class FormQuestion < ActiveRecord::Base
belongs_to :form
has_many :field_types
has_many :form_answers, dependent: :destroy
accepts_nested_attributes_for :field_types
accepts_nested_attributes_for :form_answers
end
class FormAnswer < ActiveRecord::Base
belongs_to :form_question
end
And my form controller
class FormsController < ApplicationController
def new
@form = Form.new
# form_questions = @form.form_questions.build
# form_answers = 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
解决方案
Firstly,you should uncomment
those two lines in your new
method.I guess they are correct.
def new
@form = Form.new
@form_questions = @form.form_questions.build
@form_answers = @form_questions.form_answers.build
end
And in your create
action,you are not saving
the data
def create
@form = Form.new(form_params)
if @form.save
.....
else
.....
end
end
Secondly,your form
code should look like this
<%= form_for @form do |f| %>
<div class="field">
<h1><%= @form.name %></h1>
</div>
<%= f.fields_for @form_questions do |ff| %>
<div class="field">
<%= ff.label :title %>
<%= ff.text_field :title %>
</div>
<%= ff.fields_for @form_answers do |fa| %> #Here comes the important step
<div class="field" %>
<%= fa.label :answer %>
<%= fa.text_field :answer %>
</div>
<% end %>
<% end %>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
这篇关于Rails 4 嵌套表单.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!