Rails 中多个单一形式的子项

发布于 2024-08-28 13:49:23 字数 258 浏览 2 评论 0原文

我有一个具有任意数量的子实体的模型。为简单起见,我们将实体称为“订单”和“项目”。我想要一个创建订单表单,在其中输入订单信息,并添加所需数量的商品。如果我单击“添加另一个项目”按钮,将添加一组新的表单元素来输入新数据、金额等。

我可以用纯 JavaScript 解决这个问题,但我很确定必须有一种更神奇、更滑稽的方式来做到这一点,也许有片面的观点或其他什么。我只是对 Rails 有点陌生,不知道它是什么。

动态添加新表单元素然后在创建控制器中访问它们的最佳方法是什么?

I have a model that has an arbitrary number of children entities. For simplicity lets call the entities Orders and Items. I would like to have a create Orders form where I input the order information, as well as add as many items as I want. If I click the "Add another item" button, a new set of form elements will be added to input the new data, amounts, etc..

I could hack this out in pure javascript, but I'm pretty sure there has to be a more magical, railsish way to do it, maybe with a partial view or something. I'm just a little too new to rails to know what it is.

What is the best way to dynamically add the new form elements, and then to access them in the create controller?

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

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

发布评论

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

评论(1

空心↖ 2024-09-04 13:49:23

无法超越 Ryan Bates 提供的 Railscasts.com 教程。

第 196 集:嵌套模型表单,pt。 1

这是一个仅使用单层嵌套模型的示例

models

/company.rb

class Company < ActiveRecord::Base
  has_many :people, :dependent => :destroy
  accepts_nested_attributes_for :people, :allow_destroy => true
end

models/person.rb

class person < ActiveRecord::Base
  belongs_to :company
end

控制器

company_controller.rb

def new
  @company = Company.new
  3.times { person = @company.people.build }
end

视图

views/companies/_form.html.erbviews

<% form_for @company do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>

  <%= f.fields_for :people do |builder| %> 
    <%= render "people_fields", :f => builder %>
  <% end %>

  <p><%= f.submit "Submit" %></p>
<% end %>

/companies/_people_fields.html。尔布

<p>
  <%= f.label :name, "Person" %>
  <%= f.text_field :name %>
  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove" %>
</p>

Can't beat this Railscasts.com tutorial provided by Ryan Bates.

Episode 196: Nested Model Form, pt. 1

Here's an example that works with just a single level of nesting

Models

models/company.rb

class Company < ActiveRecord::Base
  has_many :people, :dependent => :destroy
  accepts_nested_attributes_for :people, :allow_destroy => true
end

models/person.rb

class person < ActiveRecord::Base
  belongs_to :company
end

Controllers

companies_controller.rb

def new
  @company = Company.new
  3.times { person = @company.people.build }
end

Views

views/companies/_form.html.erb

<% form_for @company do |f| %>
  <%= f.error_messages %>

  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>

  <%= f.fields_for :people do |builder| %> 
    <%= render "people_fields", :f => builder %>
  <% end %>

  <p><%= f.submit "Submit" %></p>
<% end %>

views/companies/_people_fields.html.erb

<p>
  <%= f.label :name, "Person" %>
  <%= f.text_field :name %>
  <%= f.check_box :_destroy %>
  <%= f.label :_destroy, "Remove" %>
</p>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文