Partial Naming Madness

Hi, I am having the following issue with a partial. During initial page
load, I am doing a “render_partial_collect ‘foo’, @foos”, and all goes
well. Each foo partial makes use of a variable inside called ‘foo’ (and
can get foo.id etc).

After all the partials are loaded, I need to :update a div corresponding
to one of the partials (ie re-load the partial in that div). I do this
via a link_to_remote call, calling the do_stuff method in a controller.
The do_stuff method attempts to render the foo partial back, passing it
a variable like so:

render_partial ‘something/foo’, nil, ‘foo’ => Foo.find(params[:foo])

This code complains that foo is null when it renders back, however, if I
copy the _foo.rhtml code into a file called _bar.rhtml and do:

render_partial ‘something/bar’, nil, ‘foo’ => Foo.find(params[:foo])

everything works. Why is this happening?

On 8/1/06, Bryce B. [email protected] wrote:

Hi, I am having the following issue with a partial. During initial page
load, I am doing a “render_partial_collect ‘foo’, @foos”, and all goes
well. Each foo partial makes use of a variable inside called ‘foo’ (and
can get foo.id etc).

render_partial and render_partial_collect are deprecated. Use
render(:partial => …) and render(:partial => …, :collection =>
@foos) instead.

render_partial ‘something/bar’, nil, ‘foo’ => Foo.find(params[:foo])

everything works. Why is this happening?

Whenever I have to pass a variable to a partial, i use:
“render(:partial => ‘my_partial’, :locals => { ‘foo’ => some_value }”.
By default, when you render a partial, if you have an ivar with the
same name as the partial, it will be accessible inside the partial.
So if you do something like the following in do_stuff:

@foo = Foo.find(params[:foo])
render(:partial => ‘something/foo’)

then you should be able to access @foo from within the partial.

Mike

Thanks, that really helped!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs