开发者

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 selec开发者_运维技巧t 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


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜