如何创建输入模型的多个项目的表单?
我有一个用 Ruby 和 Rails 和 Hobo 编写的简单销售点应用程序。
最初打算当时仅用于一种产品,现在客户希望将多种产品添加到销售模型中
此外,我使用品牌对产品进行分类,并且在我的新销售表单中,我使用 ajax 来填充选择产品方法在另一个选择菜单中选择品牌后。
所以我想要的是使用我当前的系统,如果可能的话,只需更改我的新销售表格即可将多个产品添加到销售中
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.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜您在销售和一件产品之间存在
has_one
关系。我们的想法是改变与 的关系has_and_belongs_to_many。
因此,在您的数据库中,一次销售可以有多种产品,一种产品可以有多次销售。
对于实现,您可以使用 嵌套属性用于显示一次销售的产品并添加或删除一些产品。
I guess you have a
has_one
relation between the sale and one product.The idea would be to change that relation to a 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.
您可能需要的是将表单中发布的值从一种产品更改为多种产品。在您发布类似内容之前:
product_id=123
Product_qty=1
现在您想发布类似这样的内容
product_id[0]=123
产品数量[0]=1
产品id[1]=456
Product_qty[1]=7
或更好
product[123].qty=1
Product[456].qty=7
在您的表单中,您需要创建这些产品变量并使它们不同。然后将它们发布到您正在使用的相同表单中,但查看日志并了解 Rails 如何将它们映射到 params 对象中。然后在您的控制器中,我们使用该映射来提取多个对象。
我从 Railscast 的截屏中学到了很多东西。他们不需要花很长时间,就能看到有人在<<中解决问题”。 10分钟真是太有趣了。试试这个 http://railscasts.com/episodes/73-complex-forms -第1部分
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
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
or better yet
product[123].qty=1
product[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. Then in your controller us that mapping to pull out the multiple objects.
I've learned a ton from the railscast screen casts. They don't take long, and watching someone solve problems in < 10 minutes is SO much fun. Try this one http://railscasts.com/episodes/73-complex-forms-part-1
我必须创建一个购物车模型作为产品的容器,然后将我的销售与购物车一起使用,并从那里进一步......验证和东西必须重新编程
i had to create a cart model as a container for products, then procced to use my sale with the cart, and from there further.. validations and stuff had to be reprogrammed