如何从 Rails 中的同一页面提交多个重复的表单 - 最好使用一个按钮

发布于 2025-01-08 14:56:23 字数 362 浏览 4 评论 0 原文

在我的新视图页面中,我有:

<% 10.times do %>
  <%= render 'group_member_form' %>     
<% end %>

现在此表单包含字段:first_namelast_nameemail_addressmobile_number 。基本上,我希望能够一键填写所有表单的字段,然后将每个表单作为唯一的行/id 提交到数据库中。

实现这一目标最简单的方法是什么?

注意:从变量调用 do 的次数。欢迎任何建议,谢谢!

In my new views page I have:

<% 10.times do %>
  <%= render 'group_member_form' %>     
<% end %>

Now this form contains the fields: first_name, last_name, email_address and mobile_number. Basically I want to be able to fill in the fields of all the forms in one click which then submits each into the database as a unique row/id.

What would be the easiest way to accomplish this?

Note: The number of times do is called from a variable. Any advice welcome, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隱形的亼 2025-01-15 14:56:23

您应该只有一个表单(您应该只在 group_member_form 部分中放置字段)。在您看来,您应该有类似的内容:

<%= form_tag "/members" do %>
  <% 10.times do %>
    <%= render 'group_member_form' %>     
  <% end %>
  <%= submit_tag "Submit" %>
<% end %>

_group_member_form.html.erb 中您应该有

<%= text_field_tag "members[][first_name]" %>
<%= text_field_tag "members[][last_name]" %>
<%= text_field_tag "members[][email_address]" %>
<%= text_field_tag "members[][mobile_number]" %>

这样的方式,当表单提交时,控制器中的 params[:members] 将是成员哈希的数组。因此,例如,要在提交表单后从第四个成员那里获取电子邮件地址,您可以调用 params[:members][3][:email_adress]

要理解为什么我这样写_group_member_form.html.erb,请看一下:

http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions

You should have only one form (you should put only fields in the group_member_form partial). In your view you should have something like:

<%= form_tag "/members" do %>
  <% 10.times do %>
    <%= render 'group_member_form' %>     
  <% end %>
  <%= submit_tag "Submit" %>
<% end %>

and in _group_member_form.html.erb you should have

<%= text_field_tag "members[][first_name]" %>
<%= text_field_tag "members[][last_name]" %>
<%= text_field_tag "members[][email_address]" %>
<%= text_field_tag "members[][mobile_number]" %>

This way, when the form submits, params[:members] in the controller will be an array of member hashes. So, for example, to get the email adress from the fourth member after submitting the form, you call params[:members][3][:email_adress].

To understand why I wrote _group_member_form.html.erb like this, take a glance at this:

http://guides.rubyonrails.org/form_helpers.html#understanding-parameter-naming-conventions.

鯉魚旗 2025-01-15 14:56:23

您还可以在模型中使用accepts_nested_attributes_for,并在表单上使用fields_for。

提交多个表单,据我所知,只有 javascript,如果表单是远程的:true,并且您运行每个表单然后提交。

$("form.class_of_forms").each(function() {
  $(this).submit();
});

You can also use accepts_nested_attributes_for in your model, and use fields_for on your form.

Submitting multiple forms, afaik, only javascript, if the forms are remote: true, and you run through each of them and then submit.

$("form.class_of_forms").each(function() {
  $(this).submit();
});
陈年往事 2025-01-15 14:56:23

或者,使用 form_with 和 fields_for 的更新方法,无需将表单删除为部分,可以如下编写:

<%= form_with (url: end_point_path), remote: true do |form| %>
    <% (1..5).each do |i| %>
        <%= fields_for 'cart_items'+[i].to_s do |fields|%>
            <%= fields.text_field :first_name  %>
            <%= fields.text_field :last_name  %>
            <%= fields.email_field :email_address  %>
            <%= fields.number_field :phone_number %>
        <% end %>
    <% end %>
    <%= form.submit "Submit" %>
<% end %>

Alternatively a more up to date approach using form_with and fields_for, without removing the form into a partial, could be written like this:

<%= form_with (url: end_point_path), remote: true do |form| %>
    <% (1..5).each do |i| %>
        <%= fields_for 'cart_items'+[i].to_s do |fields|%>
            <%= fields.text_field :first_name  %>
            <%= fields.text_field :last_name  %>
            <%= fields.email_field :email_address  %>
            <%= fields.number_field :phone_number %>
        <% end %>
    <% end %>
    <%= form.submit "Submit" %>
<% end %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文