フォトギャラリー用に動的なJavaScriptを生成する必要がある、非常にシンプルなページを作成しようとしています。直感的にシンプルなものですが、私は非常に苦労しています。私のコントローラーでは、Photo
asのリストを作成@photos
してから、ファイル内の写真ごとにJavaScriptを繰り返す必要がありphoto.js.erb
ます。
ただし、.js.erbファイルに到達するたびに、@photos
変数はnilになります...何が欠けていますか?
controllers / photos_controller.rbファイル:
class PhotosController < ApplicationController
layout "application_photos"
def category
@photos = ...(generates collection of Photos)...
end
end
views / photos / category.html.hamlファイル:
= 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ファイル:
jQuery(function($){
// Throws NilClass error
<% puts 'Photos: ' + @photos %>
});
私はこの質問が何十回も聞かれたことを知っていますが、以前に受け入れられた答えのどれも実際には私のために働きませんでした。どんな提案でも大歓迎です。