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

3

11

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.

Bili answered 6/8, 2009 at 17:30 Comment(3)
What do you mean by "inaccessible"? Does it throw a undefined local variable or method or is it nil? Can you show the code that uses foo on these sub-partials?Fontes
it throws undefined local variableBili
the code was simple: <%= foo %>Bili
B
3

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

Bili answered 6/8, 2009 at 18:3 Comment(3)
I actually had the same problem, and my solution was exactly the same -- I figured it out when I saw your mistake :PRapallo
ugh me too! ha. I changed my haml from: "- if myvar" to "- if defined?(myvar) && myvar"Gearwheel
same here, was passing the partial in AJAX, forgot to change the symbolLyly
S
41

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.

Salena answered 5/12, 2010 at 3:9 Comment(4)
+1 @Salena Strange that you need :partial to make it work, but this does indeed work!Breakdown
wow!!! Thanks ... I am just learning Ruby and I was totally confused regarding this error. I wish I could mod-up more points.Schrock
This solved my issue as well but I'm really curious why the render partial: "partial_name", locals: { foo: "bar" } method has to be used. I understand that we are actually rendering a partial. What I don't understand is how that differs from a view.Longstanding
still useful in 2015!Ellora
B
3

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

Bili answered 6/8, 2009 at 18:3 Comment(3)
I actually had the same problem, and my solution was exactly the same -- I figured it out when I saw your mistake :PRapallo
ugh me too! ha. I changed my haml from: "- if myvar" to "- if defined?(myvar) && myvar"Gearwheel
same here, was passing the partial in AJAX, forgot to change the symbolLyly
V
0

Bumped into this very old question cause I faced the same issue. Turned out that with Rails 4+ if you are not using collections or layout the correct way is:

# Instead of <%= render partial: "account", locals: { account: @buyer } %>
<%= render "account", account: @buyer %>

As documented here.

Vaccine answered 8/8, 2017 at 10:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.