开发者

Rails has_many ajax

开发者 https://www.devze.com 2023-03-26 07:15 出处:网络
Lets say I have a recipe that has_many ingredients, I want the recipe creating page to have forms for each ingredient. How can I make it where a user can click a button to add a new ingredient then sa

Lets say I have a recipe that has_many ingredients, I want the recipe creating page to have forms for each ingredient. How can I make it where a user can click a button to add a new ingredient then save all the ingredients and make them belong to th开发者_JAVA技巧e recipe?


The main idea here is to have a link on your recipe creation page that says something like "Add Ingredient" and give it a unique class. Then use jQuery to add an AJAX action to the click method of those links that will get the page used to create an ingredient. When you you hit Submit, it will come in the params as well. You may have to fiddle around with the IDs of stuff in order to make the the params hash work out nicer.

Let me know if that's enough to get you going or if you want some code examples too.


You're going to want to use accepts nested attributes and build. See Ryan Bates Railscast and just add ajax to it.

Here

And Here

0

精彩评论

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