Forum: Ruby on Rails Instance or local vars for field helpers in partial template

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Julian G. (Guest)
on 2006-05-05 01:24
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
Wilson B. (Guest)
on 2006-05-05 01:34
(Received via mailing list)
On 5/4/06, Julian G. <removed_email_address@domain.invalid> wrote:
> the controller.
> If I have a field helper, I cannot now use:
>
Not that I'm aware of.  That's the way DHH told me to do it. Heh.
This topic is locked and can not be replied to.