开发者

Build rails form fields from associated models

开发者 https://www.devze.com 2023-01-26 01:16 出处:网络
Rails3,jQuery. What I\'m trying to do: Create a new WorkoutPlan that HABTM Exercises.Then create a new Workout that belongs_to a WorkoutPlan.I want the Workout#edit page to build a form with fields_fo

Rails3,jQuery. What I'm trying to do: Create a new WorkoutPlan that HABTM Exercises. Then create a new Workout that belongs_to a WorkoutPlan. I want the Workout#edit page to build a form with fields_for new/edi开发者_运维技巧t WorkoutRoutines, one routine for each exercise in the WorkoutPlan.

That last part is where I'm struggling, creating one Routine for each Exercise in the WorkoutPlan. Everything else works, its just this form build that sucks. Not sure if I'm making this overly complicated, or if there was an easier way. Any ideas?


From what I understand, using nested attributes should solve the problem without many complications. From the link,

Nested attributes allow you to save attributes on associated records through the parent. By default nested attribute updating is turned off, you can enable it using the accepts_nested_attributes_for class method. When you enable nested attributes an attribute writer is defined on the model.

The attribute writer is named after the association, which means that in the following example, two new methods are added to your model:

author_attributes=(attributes) and pages_attributes=(attributes).


anytime you need a complex form in rails its either going to be a pain, or can use formtastic. super easy awesome forms, plenty of ways to deal with habtm relations as well.

0

精彩评论

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