0

I'm trying to pass a local variable to the partial:

<%= render :partial => 'bar/search_bar', :locals => { :foo => "1111" }  %>

but when I try to access the foo variable in the partial

<p> <%= foo %> </p>

I get:

ActionView::Template::Error (undefined local variable or method `foo' for #<#<Class:0x007f32249423e0>:0x007f3230b839b8>):

I'm using rails 3.2, I also followed some posts but they couldn't help either.

tokhi
  • 21,044
  • 23
  • 95
  • 105
  • 2
    @Trip That's wrong, that isn't how you access locals. The syntax in both snippets of code is exactly correct, there is something else at play here. – user229044 Feb 25 '14 at 14:11
  • 1
    He could also try `<%= render :partial => 'bar/search_bar', :foo => "1111" %>` I believe.. – Trip Feb 25 '14 at 14:12
  • 2
    @Trip Also no. The two syntaxes that can work here are `render 'bar/search_bar', foo: '1111` *or* `render partial: 'bar/search_bar', locals: { foo: '1111'} }`. You use `:parial`/`:locals`, or you just use a string and implicit `:locals` hash. – user229044 Feb 25 '14 at 14:13
  • Maybe you render this partial in few places and in someplace you forgot to pass the local parameter? – Anton Tsapov Feb 25 '14 at 14:29
  • I sometimes ran into this problem when accessing the local variable in the very first line of the partial. I don't really know why this should be a problem but my debugger confirmed it. Might be worth a try if your ´

    ´ line is the very first one...

    – KappaNossi Feb 25 '14 at 14:30
  • @KappaNossi: I placed it at the very bottom, still the same. – tokhi Feb 25 '14 at 14:34
  • 1
    Does the _search_bar.html.erb partial pull in any other partials? Also, can you put up the stacktrace? That will probably help – rainkinz Feb 25 '14 at 14:50
  • is it possible that you render the wrong partial? `<%= render :partial => 'bar/search_bar' %>` in an other file? – xlembouras Feb 25 '14 at 15:16
  • @rainkinz: thanks, I was rendering another partial via javascript. problem resolved. many thanks. – tokhi Feb 25 '14 at 15:24

0 Answers0