6

I am trying to access an instance variable from a js.erb file.

#controller
def get_person
  @person = Person.find(1)

  respond_to do |format|
   format.js{}
  end
end

#get_person.js.erb
alert('<%= @person.last_name %>')

When I browse to [controller_name_here]/get_person.js ... I get a nil object error on @person. (I know Person.find(1) returns an object)

Note: I am actually having trouble rendering a partial in my js.erb file and am trying to identify the cause.

6
  • Is the problem in the partial then? If so, you should know that partials don't have access to the class variables of their caller. You need to pass in anything that you want it to have access to. Commented Jun 3, 2009 at 19:15
  • Since I am creating the partial in my js.erb file, shouldn't my js.erb file have access to class variables? Commented Jun 3, 2009 at 19:20
  • No. You have to pass them in as locals: <%= render(:partial => 'partials/foo', :locals => {:whatwhat => @somedata}) %> Commented Jun 3, 2009 at 19:28
  • 2
    As an aside, you don't need the empty braces in the format.js line. Commented Jun 3, 2009 at 19:34
  • ok, so I tried rendering the partial in the js.erb files like so: $("#members").append("<%= escape_javascript(render(:partial => 'partialname', :locals => {:person => @person} ))%>"); but in partial I still an getting a nil object error on person....this is what led me to try and break down the problem. Commented Jun 3, 2009 at 19:40

2 Answers 2

5

The following works for me:

In /app/controllers/foo_controller.rb:
class FooController < ApplicationController
  def get_person
    @person = Person.find(1)
    respond_to do |format|
      format.js
    end
  end
end
In /app/views/foo/get_person.js.erb:
<%= render :partial => '/foo/some_partial', :locals => { :person => @person } %>
In /app/views/foo/_some_partial.js.erb:
person = {
  last_name: '<%= person.last_name -%>'
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is strange, instance variables are intended to be visible in all partials so the locals construct should not be necessary (in this particular case). There is something else amiss here.
that's really strange. I get "The error occurred while evaluating nil.last_name"
Lee: you're sure you're using "<%= person.last_name -%>" and not "<%= @person.last_name -%>" in the partial?
Lee: if you are using person.last_name in the partial and you're still seeing "The error occurred while evaluating nil.last_name" (instead of "undefined local variable or method 'person'"), then the Person.find(1) is your problem!
0

There is no more rendering partials inside your js.erb file. I just discovered this today.

https://github.com/rails/rails/issues/1370

Comments

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.