如何在 Rails 中以嵌套形式省略现有子记录?

发布于 2024-08-22 17:14:32 字数 826 浏览 6 评论 0原文

在我的应用程序中,用户有许多项目。我想创建一个“添加多个项目”表单,以便用户可以一次创建多个项目

在我看来,最快的方法是制作一个用户表单,其中嵌套有项目字段,并省略用户字段。这样,当提交表单时,将保存用户并自动创建所有新的项目记录。

但是,我不希望现有项目显示在表单中。只有正在创建的新项目(来自@user.projects.build)的空字段。是否有我可以传递的参数或我可以在表单中更改的内容以省略现有的项目记录?

<% form_for (@user) do |f| %>

   <% f.fields_for :project do |project_form| %>
      <%= render :partial => 'project', :locals => {:f => project_form}  %>
   <% end %>

   <%= add_child_link "New Project", f, :projects %>

   <%= f.submit "save" %> 

<%end%>

我正在使用 Ryan Bate 的复杂表单示例。该代码运行良好。我只是想忽略以这种形式显示的现有项目。

In my application an User has many Projects. I want to create a "add many projects" form, so the User can create many Projects at once.

It seemed to me that the quickest way was to make a User form with Project fields nested in it, and omit the User fields. This way when the form is submitted, the User is saved and all the new Project records are created automatically.

However, I don't want the existing Projects to show in the form. Only the empty fields for the new project that is being created (from @user.projects.build). Is there a parameter I can pass or something I can change in the form to omit the existing Project records?

<% form_for (@user) do |f| %>

   <% f.fields_for :project do |project_form| %>
      <%= render :partial => 'project', :locals => {:f => project_form}  %>
   <% end %>

   <%= add_child_link "New Project", f, :projects %>

   <%= f.submit "save" %> 

<%end%>

I'm using the Ryan Bate's complex forms example. The code works fine. I just want to omit the existing projects from showing up in this form.

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

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

发布评论

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

评论(2

梦幻之岛 2024-08-29 17:14:32

您可以使用 new_record? 方法来区分新创建的记录和旧记录:

<% form_for @user do |f| %>
   <% f.fields_for :project do |project_form| %>
      <%= render :partial => 'project', :locals => {:f => project_form} if project_form.object.new_record? %>
   <% end %>
   <%= add_child_link "New Project", f, :projects %>
   <%= f.submit "save" %> 
<% end %>

You can use new_record? method to distinguish between newly created record and old one:

<% form_for @user do |f| %>
   <% f.fields_for :project do |project_form| %>
      <%= render :partial => 'project', :locals => {:f => project_form} if project_form.object.new_record? %>
   <% end %>
   <%= add_child_link "New Project", f, :projects %>
   <%= f.submit "save" %> 
<% end %>
债姬 2024-08-29 17:14:32

您可以尝试

  <% f.fields_for :project, Project.new do |project_form| %>
    <%= render :partial => 'project', :locals => {:f => project_form}  %>
  <% end %>

为一条记录提供空白字段。

在控制器中,您可以为关系生成多个记录,

 5.times { @user.projects.build }

这将创建五个与用户相关的新空项目,并且您当前的 fields_for 将包含新记录的字段。

You can try

  <% f.fields_for :project, Project.new do |project_form| %>
    <%= render :partial => 'project', :locals => {:f => project_form}  %>
  <% end %>

that should give you blank fields for one record.

In the controller you can generate multiple records for the relationship

 5.times { @user.projects.build }

This will make five new empty projects related to the user and your current fields_for will have fields for new records.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文