Julian Gall
2006-May-04 21:25 UTC
[Rails] Instance or local vars for field helpers in partial template
I''m getting confused. If I have a view (.rhtml), I can use a field helper like: <%= text_field :mymodel, :attribute %> This uses the value from @mymodel.attribute Now, if I am rendering a template normally (i.e. not partial), I would expect @mymodel to be a model object defined as an instance variable in the controller. However, if I am rendering a partial template, I either have: render :partial => ''mything'', :object => @mymodel or render :partial => ''mything'', :collection => @mymodelcollection Now, inside the template, the @mymodel object is available as the *local* variable mything (same as template name). If I have a field helper, I cannot now use: text_field :mything, :attribute because text_field expects @mything (an instance variable). I can get around this my putting at the top of my partial template: <% @mything = mything %> but this seems a little clunky and un-Ruby-ish. Is there a better way? Thanks, Julian -- Posted via http://www.ruby-forum.com/.
Wilson Bilkovich
2006-May-04 21:34 UTC
[Rails] Instance or local vars for field helpers in partial template
On 5/4/06, Julian Gall <julian.gall@gmail.com> wrote:> I''m getting confused. > > If I have a view (.rhtml), I can use a field helper like: > > <%= text_field :mymodel, :attribute %> > > This uses the value from @mymodel.attribute > > Now, if I am rendering a template normally (i.e. not partial), I would > expect @mymodel to be a model object defined as an instance variable in > the controller. > > However, if I am rendering a partial template, I either have: > > render :partial => ''mything'', :object => @mymodel > or > render :partial => ''mything'', :collection => @mymodelcollection > > Now, inside the template, the @mymodel object is available as the > *local* variable mything (same as template name). > > If I have a field helper, I cannot now use: > > text_field :mything, :attribute > > because text_field expects @mything (an instance variable). > > I can get around this my putting at the top of my partial template: > > <% @mything = mything %> > > but this seems a little clunky and un-Ruby-ish. Is there a better way? >Not that I''m aware of. That''s the way DHH told me to do it. Heh.