I''ve got the classic Person "has an" Address set up. I want the end user to be able to edit the person and their address at the same time. As a result, person/edit.rhtml looks like this. <!-- person/edit.rhtml --> <h1>Editing person</h1> <%= start_form_tag :action => ''update'', :id => @person %> <%= render_partial "form" %> <%= render_partial "addresses/form", @person.address %> <%= submit_tag "Edit" %> <%= end_form_tag %> <%= link_to ''Show'', :action => ''show'', :id => @person %> | <%= link_to ''Back'', :action => ''list'' %> <!-- end person/edit.rhtml --> That fails with the following: ActionView::TemplateError (undefined method `errors'' for nil:NilClass) on line #1 of /addresses/_form.rhtml: 1: <%= error_messages_for ''address'' %> 2: 3: <!--[form:address]--> OK, I thought, that''s natural enough if @person.address is nil. So I tried with a Person who had an address and got the same error. I then tried <%= render_partial "addresses/form", Address.new %> and got the same error. Is this kind of "composition of edit forms" not encouraged? It seems to me like the best way of DRY. What am I missing? How should I be tackling this? Cheers, Jim
After an IRC session with the ever helpful ''bitsweat'', I''m following up my own post. The problem is that render_partial had some expectations that I didn''t necessary share. The call to <%= render_partial "addresses/form", @person.address %> sticks the @person.address variable into a variable named ''form'' for the duration of the partial. In short, the first argument to render_partial indicates not only what file on the filesystem will be used for rendering, but also the variable name that your passed parameter will take. I had gotten by on luck with this in a couple of other situations since the partial was named the same as my variable. The render_partial documentation does make this clear, but for all those relative noobs like me who read the list for tips, I thought I''d share. One may be able to do something like <%= render_partial ''foo/something_with_a_horrible_name'', :myvar => some_value %> as well. Make sure to check that out. Cheers, Jim Jim Van Fleet wrote:> I''ve got the classic Person "has an" Address set up. I want the end > user to be able to edit the person and their address at the same time. > > As a result, person/edit.rhtml looks like this. > > <!-- person/edit.rhtml --> > > <h1>Editing person</h1> > > <%= start_form_tag :action => ''update'', :id => @person %> > <%= render_partial "form" %> > <%= render_partial "addresses/form", @person.address %> > > <%= submit_tag "Edit" %> > <%= end_form_tag %> > > <%= link_to ''Show'', :action => ''show'', :id => @person %> | > <%= link_to ''Back'', :action => ''list'' %> > > <!-- end person/edit.rhtml --> > > That fails with the following: > > ActionView::TemplateError (undefined method `errors'' for nil:NilClass) > on line #1 of /addresses/_form.rhtml: > 1: <%= error_messages_for ''address'' %> > 2: > 3: <!--[form:address]--> > > > OK, I thought, that''s natural enough if @person.address is nil. So I > tried with a Person who had an address and got the same error. I then > tried <%= render_partial "addresses/form", Address.new %> and got the > same error. > > Is this kind of "composition of edit forms" not encouraged? It seems to > me like the best way of DRY. What am I missing? How should I be > tackling this? > > Cheers, > > Jim > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
see: http://www.pointstorm.com/~gavin/partials-doc/classes/ActionView/Partials.html but in general... the version of render_partial you are using bases the variable name off of the template name the render_partial ''template'', nil, ''address'' => @person.address would be better but for that to work with error_messages_for you need to add <%# @address = address if defined? address %> to you partial then you can do <%= error_messages_for ''address'' %> that should solve your update view problem... and you have the other part hadled... Jim Van Fleet wrote:> I''ve got the classic Person "has an" Address set up. I want the end > user to be able to edit the person and their address at the same time. > > As a result, person/edit.rhtml looks like this. > > <!-- person/edit.rhtml --> > > <h1>Editing person</h1> > > <%= start_form_tag :action => ''update'', :id => @person %> > <%= render_partial "form" %> > <%= render_partial "addresses/form", @person.address %> > > <%= submit_tag "Edit" %> > <%= end_form_tag %> > > <%= link_to ''Show'', :action => ''show'', :id => @person %> | > <%= link_to ''Back'', :action => ''list'' %> > > <!-- end person/edit.rhtml --> > > That fails with the following: > > ActionView::TemplateError (undefined method `errors'' for nil:NilClass) > on line #1 of /addresses/_form.rhtml: > 1: <%= error_messages_for ''address'' %> > 2: > 3: <!--[form:address]--> > > > OK, I thought, that''s natural enough if @person.address is nil. So I > tried with a Person who had an address and got the same error. I then > tried <%= render_partial "addresses/form", Address.new %> and got > the same error. > > Is this kind of "composition of edit forms" not encouraged? It seems > to me like the best way of DRY. What am I missing? How should I be > tackling this? > > Cheers, > > Jim > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails