title: Rails 3: Instance variable inaccessible in .js.erb file
body: <p>I'm trying to build a really simple page, where I need to generate some dynamic JavaScript for a photo gallery. Something intuitively simple, but which I'm struggling with greatly. In my controller I am building a list of <code>Photo</code> as <code>@photos</code>, then need to iterate out some JavaScript for each photo in my <code>photo.js.erb</code> file.</p>
  
  <p>However, whenever I reach my .js.erb file, the <code>@photos</code> variable becomes nil... what am I missing?</p>
  
  <p><strong>controllers/photos_controller.rb</strong> file:</p>
  
  <pre><code>class PhotosController &lt; ApplicationController
    layout "application_photos"
    def category
      @photos = ...(generates collection of Photos)...
    end
  end
  </code></pre>
  
  <p><strong>views/photos/category.html.haml</strong> file:</p>
  
  <pre><code>= content_for :head do
    // @photos is still initialized at this point
    = javascript_include_tag "photos"
  
  // This partial only loads some more markup, it's inconsequential.
  = render :partial =&gt; 'default_slideshow'
  </code></pre>
  
  <p><strong>javascripts/photos.js.erb</strong> file:</p>
  
  <pre><code>jQuery(function($){
      // Throws NilClass error
      &lt;% puts 'Photos: ' + @photos %&gt;
  });
  </code></pre>
  
  <p>I know this question has been asked a dozen times, but none of the previously accepted answers actually worked for me. Any suggestions are greatly appreciated.</p>