Rails多记录表单仅保存最后一条记录的参数 [英] Rails multi-record form only saves parameters for last record
问题描述
我正在尝试为老师提供一种表格,该表格可以一次创建多个学生.似乎大多数人都使用嵌套属性来解决这个概念,但是当我仅使用一个模型时,我很难理解它是如何工作的. 这篇文章似乎可以在没有嵌套属性的情况下实现这一点,但是我的结果无法按作者建议的方式工作.学生数组应为表单的每个部分都包含一个哈希.但是,当我提交表单并检查参数时,数组中仅存在一个哈希.
I'm trying to offer teachers a form that will create multiple students at once. It seems that most people tackle this concept with nested attributes, but I'm having a hard time understanding how that would work when I'm only using a single model. This article made it seem possible to achieve this without nested attributes, but my results are not working the way the author suggests. The students array should include one hash for each section of the form. But when I submit the form and check the parameters, only one single hash exists in the array.
调整她的方法,我有了这个控制器:
Adjusting her approach, I've got this controller:
students_controller.rb
def multi
@student_group = []
5.times do
@student_group << Student.new
end
end
(我使用的是我称为多"动作,因为它与常规创建"动作的视图不同,后者只能一次创建一个学生.我尝试将所有内容移入常规创建动作,但结果相同.)
(I'm using an action I've called "multi" because it's a different view than the regular "create" action, which only creates one student at a time. I've tried moving everything into the regular create action, but I get the same results.)
视图:
multi.html.erb
<%= form_tag students_path do %>
<% @student_group.each do |student| %>
<%= fields_for 'students[]', student do |s| %>
<div class="field">
<%= s.label :first_name %><br>
<%= s.text_field :first_name %>
</div>
<div class="field">
<%= s.label :last_name %><br>
<%= s.text_field :last_name %>
</div>
<% end %>
<% end %>
<div class="actions">
<%= submit_tag %>
</div>
<% end %>
结果:
(byebug) params
<ActionController::Parameters {"utf8"=>"✓", "authenticity_token"=>"3Xpi4XeqXuPs9jQvevy+nvGB1HiProddZzWq6Ed7Oljr3TR2fhx9Js6fN/F9xYcpgfDckCBOC2CoN+MrlFU0Bg==", "students"=>{"first_name"=>"fff", "last_name"=>"ggg"}, "commit"=>"Save changes", "controller"=>"students", "action"=>"create"} permitted: false>
只有一个名叫"fff ggg"的学生可以参加.还应该有其他四个哈希值与不同的学生一起使用.
Only one has is included for a student named "fff ggg". There should be four other hashes with different students.
在此先感谢您的见解.
推荐答案
fields_for
仅与form_for
结合使用. for
指的是您希望使用的模型.由于您尝试构建没有模型的表单,因此必须构建自己的输入字段名称.
fields_for
is only used in conjunction with form_for
. The for
is referring to a model, which it expects you to use. Since you're trying to build a form with no model, you have to construct your own input field names.
不要使用fields_for
,而是使用表单标签帮助程序来呈现每个输入,例如
Don't use fields_for
but instead, render each input using the form tag helpers e.g.
<%= label_tag "students__first_name", "First Name" %>
<%= text_field_tag "students[][first_name]" %>
...等等.
关键是字段名称中带有[]
,以指示学生参数将是一个哈希数组.通过告诉fields_for
被称为students[]
几乎可以得到它,但是fields_for
忽略了它,因为它需要模型才能正常工作.
The key is that the field names have that []
in them to indicate that the students parameters will be an array of hashes. You almost got it by telling fields_for
to be called students[]
but fields_for
ignored it because it needs a model to work correctly.
这篇关于Rails多记录表单仅保存最后一条记录的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!