简体   繁体   English

如何创建输入模型的多个项目的表单?

[英]How do i create a form that inputs multiple items of a model?

I have a simple point of sale application written in ruby and rails, and hobo. 我有一个用红宝石,铁轨和流浪汉编写的简单销售点应用程序。

Originally intended to be for only one product at the time, now the client wants to add multiple products into the sale model 最初原本只打算同时销售一种产品,现在客户希望将多种产品添加到销售模型中

Besides that i am using brands for categorizing products and in my new sale form i use ajax in order to populate a select product method after selecting the brand in another select menu. 除此之外,我使用品牌对产品进行分类,在我的新销售表格中,我使用ajax,以便在另一个选择菜单中选择品牌后填充选择产品的方法。

so what i want is to use my current system and just change my new sale form if possibly to add multiple products to a sale 所以我想要的是使用当前的系统,如果可能将多个产品添加到销售中,只需更改新的销售表格

I guess you have a has_one relation between the sale and one product. 我猜您在销售和一种产品之间具有has_one关系。
The idea would be to change that relation to a has_and_belongs_to_many . 想法是将这种关系更改为has_and_belongs_to_many
So in your database, you could have many products for one sale and many sales for one product. 因此,在数据库中,您可能有一个销售的许多产品和一个产品的许多销售。

And for the implementation, you can use nested attributes to display the products for one sale and add or remove some. 对于实现,您可以使用嵌套属性来显示要出售的产品,并添加或删除其中一些产品。

What you're probably looking for is to change the values that get posted from the form from one product to many. 您可能正在寻找的是将表单中发布的值从一种产品更改为多种。 Before you probably posted something like this: 在您可能发布这样的内容之前:

product_id=123 product_qty=1 product_id = 123 product_qty = 1

and now you want to post something like this 现在您想发布这样的内容

product_id[0]=123 product_qty[0]=1 product_id[1]=456 product_qty[1]=7 product_id [0] = 123 product_qty [0] = 1 product_id [1] = 456 product_qty [1] = 7

or better yet 或更好

product[123].qty=1 product[456].qty=7 产品[123] .qty = 1产品[456] .qty = 7

In your form, you'll need to create these product variables and make them different. 在您的表单中,您需要创建这些产品变量并使它们不同。 Then post them to the same form you're using, but look at the logs and see how rails is mapping them into the params object. 然后将它们发布到您使用的相同表单上,但请查看日志并了解rails如何将它们映射到params对象中。 Then in your controller us that mapping to pull out the multiple objects. 然后在您的控制器中使用我们的映射来提取多个对象。

I've learned a ton from the railscast screen casts. 我从railscast屏幕上学到了很多东西。 They don't take long, and watching someone solve problems in < 10 minutes is SO much fun. 他们用不了多长时间,看着某人在不到10分钟的时间内解决问题真是太有趣了。 Try this one http://railscasts.com/episodes/73-complex-forms-part-1 试试这个http://railscasts.com/episodes/73-complex-forms-part-1

我必须创建一个购物车模型作为产品的容器,然后才能将其与购物车一起使用,然后从那里开始..验证和物料必须重新编程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为关联的 model 创建表单? - How do I create a form for an associated model? 如何在单击按钮时使表单中的多个输入显示为其他表单上的选择选项值? - How do I make multiple inputs in a form appear as a select option value on different form on button click? 如何计算表单中的用户输入? - how do i calculate user inputs in form? 如何生成表格形式的多个输入? - How do I generate multiple inputs in formtastic? Rails 3 / Form没有Model:如何创建一个与模型无关的表单? - Rails 3 / Form without Model: How do I create a form that is not tied to a model? 如何在Rails中为同一表单创建多个提交按钮? - How do I create multiple submit buttons for the same form in Rails? 如何以一种形式创建多个记录? - How do I create multiple records in one form? ia 如何用 div 标签包围来自表单的多个输入? - How can i a surround multiple inputs from a form with a div tag? 如何创建一个嵌套表单,该表单的字段列表的长度取决于另一个模型的计数? - How do I create a nested form that has a list of fields whose length is dependent on the count of another model? Rails 4,如何从表单中的字符串字段创建新的模型实例? - Rails 4, How do I create a new model instance from a string field in a form?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM