Hello: How do you create a composite view? I have a form based on the model "contacts" which consists the following models: - organization - person - address I was going to use partials to create the form; e.g: - organizations/_form.rhtml - people/_form.rhtml .etc ...and render them with the appropriate tag: <%= render_partial ''people/form'', @person %> I wanted to pass a person for use with the text field helper: <%= text_field ''person'', ''first_name'' %> In my contact controller I had to ammend the "new" method to this: def new @contact = Contact.new @person = Person.new end Which seemed a little ugly to me - no specific reason, just a hunch. So my question is - are there any patterns for creating a composite views in Rails? Cheers, Nicholas
Hello again: No answer on this from the list, so I thought I would reword my question as it might not be that clear. Basically I have a model "contact" consisting of a number of other models (organization, person, address). I have a form for creating a "contact" which allows the end user to enter the organization, person and address information all at once to create the contact. I guess you can call this a composite view. I have had a look at a few Rails examples and tutorials and haven''t seen any examples. I''m sure I can work it out, but I was wondering if there was a best practice/pattern for this? Cheers, Nicholas --- Nicholas Henry FirstHand Web Design -- http://www.firsthand.ca Landmark Ottawa Stock Photography -- http://www.landmarkottawa.ca On 7/6/05, Nicholas Henry <nicholas.henry-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello: > > How do you create a composite view? I have a form based on the model > "contacts" which consists the following models: > - organization > - person > - address > > I was going to use partials to create the form; e.g: > - organizations/_form.rhtml > - people/_form.rhtml .etc > > ...and render them with the appropriate tag: > > <%= render_partial ''people/form'', @person %> > > I wanted to pass a person for use with the text field helper: > > <%= text_field ''person'', ''first_name'' %> > > In my contact controller I had to ammend the "new" method to this: > > def new > @contact = Contact.new > @person = Person.new > end > > Which seemed a little ugly to me - no specific reason, just a hunch. > > So my question is - are there any patterns for creating a composite > views in Rails? > > Cheers, > Nicholas >
Since no one else has answered yet, I''ll throw in my own 2 cents.> How do you create a composite view? I have a form based on the model > "contacts" which consists the following models: > - organization > - person > - address > > I was going to use partials to create the form; e.g: > - organizations/_form.rhtml > - people/_form.rhtml .etc> ...and render them with the appropriate tag: > > <%= render_partial ''people/form'', @person %>I see nothing wrong with that approach. No sense in having multiple forms out there all performing function.> I wanted to pass a person for use with the text field helper: > > <%= text_field ''person'', ''first_name'' %> > > In my contact controller I had to ammend the "new" method to this: > > def new > @contact = Contact.new > @person = Person.new > end > > Which seemed a little ugly to me - no specific reason, just a hunch.I''m no expert, but I think this has to do with the fact that the form is reused for ''edit'' (which requires @contact, @person,etc to be set), and also used in validation (for repopulating the fields with already entered data). If you look at the scaffold code generated by rails, you''ll see in the forms that they use @contact, @person, etc, and so for ''new'' to use these partials, it needs to define @model variables.> So my question is - are there any patterns for creating a composite > views in Rails?I think partials are the way to go. Elsewhere in your app, render_component may also fullfill this function for you. Matt