0

I've seen the other posts, but can't seem to figure out why this isn't working for me

In my controller I set

  @referrer=referrer.name

in my view i have

  <%= render 'js', :referrer => @referrer >

then in my partial i put

   var type =' <%= referrer >';

I get a response 'undefined local variable or method 'referrer' and it points to the _js file.

from what I can see, this is exactly how it is supposed to be written, what am I doing wrong?

2 Answers 2

2
<%= render :partial => "account", :locals => { :referrer => @referrer } %>

Your variables to use in the partial need to be passed via the :locals hash.

Reference: http://api.rubyonrails.org/classes/ActionView/Partials.html

EDIT

The following works perfectly for me:

Controller:

  def index
    @referrer = "test"
  end

index.html.erb

<%= render :partial => "account", :locals => { :referrer => @referrer } %>

_account.html.erb

<%=referrer%>
Sign up to request clarification or add additional context in comments.

4 Comments

I was pretty sure that the :partial and :locals had been deprecated for Rails3.
Not as far as I know. See the partials section of this page: guides.rubyonrails.org/layouts_and_rendering.html "In previous versions of Rails, the default local variable would look for an instance variable with the same name as the partial in the parent. This behavior was deprecated in 2.3 and has been removed in Rails 3.0."
seems you are right, looking at this documentation also guides.rubyonrails.org/…, but apparently it isn't needed. Any idea why I have to use @referrer in my view? I thought I would be using 'referrer', I'll accept your answer if you can explain that one, as that is what has me stumped.
I am not sure, I just made a test app and it works fine. I can stick it on github if you want to check it out, but the code is the same as in my answer.
0

Turns out the reason was that in the partial, I had to call

  var type='<%= @referrer %>'

not sure why all the other documentation i'd seen had it without the @ symbol

1 Comment

This works but the locals hash proposed by @Gazier is recommended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.