Rails 中多个单一形式的子项
我有一个具有任意数量的子实体的模型。为简单起见,我们将实体称为“订单”和“项目”。我想要一个创建订单表单,在其中输入订单信息,并添加所需数量的商品。如果我单击“添加另一个项目”按钮,将添加一组新的表单元素来输入新数据、金额等。
我可以用纯 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无法超越 Ryan Bates 提供的 Railscasts.com 教程。
第 196 集:嵌套模型表单,pt。 1
这是一个仅使用单层嵌套模型的示例
models
/company.rb
models/person.rb
控制器
company_controller.rb
视图
views/companies/_form.html.erbviews
/companies/_people_fields.html。尔布
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
models/person.rb
Controllers
companies_controller.rb
Views
views/companies/_form.html.erb
views/companies/_people_fields.html.erb