views:

76

answers:

4

I'm new to rails, so be nice.

I'm building a "rolodex" type application, and this question is about the best way to handle creating an entity along with several relationship entities at the same time.

For (a contrived) example: My application will have a Person model, which has_one Contact_Info model. On the create.html.erb page for Person it makes sense for the user of my appliction to create the person, and the contact_info at the same time.

It doesn't seem right to include details for creating a contact directly in the create view/controller for person. What's the rails way to handle this?

+1  A: 

Using nested attributes is the most common way to do this.

The actual documentation is here.

x1a4
A: 

You want to use "Nested Forms". There is a great example of them in this blog post.

Mike Trpcic
A: 

Hi there. I'm also noob, but I had a similar issue with an app. I was using a tutor at the time and he basically said it was a good example of rails being opinionated. It sounds like you want to take the create action for two different models at the same time, which may be possible but probably very hard. Id suggest considering whether your data model could be modified, or find a way to make an acceptable user flow while collecting the data in different forms.

Update: while writing this the technical answer came in. Keep in mind, its perfectly okay to take the easy route if doing so helps you get the app out the door, and especially while you're still new.

Andrew