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 Photo as @photos, then need to iterate out some JavaScript for each photo in my photo.js.erb file.
However, whenever I reach my .js.erb file, the @photos variable becomes nil... what am I missing?
controllers/photos_controller.rb file:
class PhotosController < ApplicationController
layout "application_photos"
def category
@photos = ...(generates collection of Photos)...
end
end
views/photos/category.html.haml file:
= 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 => 'default_slideshow'
javascripts/photos.js.erb file:
jQuery(function($){
// Throws NilClass error
<% puts 'Photos: ' + @photos %>
});
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.
category.html.hamlview and then jQueryize what's on the page? do you really need to access@photosfrom the JS? If you really need to, then you'll probably have to render the.js.erbas a view from the controller.