Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails partial locals not persisting when sent to another partial as its own local

I render a partial like so:

<%= render :partial => 'widgets/some_partial, :locals => {:foo => 'bar'} %>

So inside of _some_partial.html.erb I render two more partials like so:

<% #foo.nil? #=> false %>
<%= render :partial => 'widgets/another_partial', :locals => {:foo => foo} %>
`<%= render :partial => 'widgets/another_partial_again', :locals => {:foo => foo} %>`

The foo local variable renders fine in some_partial.html.erb and even in another_partial_again.html.erb. However, the foo variable is inaccessible in another_partial.html.erb even though I explicitly passed it in the render call.

What is happening here?

Thanks for the help.

like image 911
user94154 Avatar asked Aug 06 '09 17:08

user94154


2 Answers

I had the undefined local variable or method error come up for me too when I was rendering a partial with :locals defined.

However, I had a different issue causing my problem, so I thought I would share my solution in case it helps anyone else. (This page was the first result when I googled this error after all)

Basically just make sure you use :partial => 'path/to/partial' in your call to render.

I.e.

<%= render :partial => 'widgets/some_partial', :locals => {:foo => 'bar'} %>

NOT like I was doing:

<%= render 'widgets/some_partial', :locals => {:foo => 'bar'} %>

Easy for a rails/ruby newbie like me to miss.

like image 178
asgeo1 Avatar answered Nov 09 '22 06:11

asgeo1


Solved. Turns out I was also rendering the same partial from the controller without sending the proper local variables. Thanks anyways!!!

like image 3
user94154 Avatar answered Nov 09 '22 07:11

user94154