You will need to display it as partial and pass the resource as local:
<script> <%= render :partial => 'resources/gallery_resource', :locals => { :resource => @resource } %> </script>
In addition, in the gallery_resource.js.erb file, you will need to change @resource only the resource:
element.image = "<%= asset_path 'resources/galleries/'+resource.uid+'/'+index+'.jpg' %>"
If this is visualized in any other actions, they also need to pass the value of the resource: in the locals hash.
The volume of js.erb files depends on several things:
- If the controller itself renders the file, it has access to instance variables (for example, "@resource"). This will be the case with view / viewtype / show.js.erb if it exists and if the browser requests a js response type. Like the .html.erb file.
- If it is displayed as partial, then the only area is what you pass with the hash: locals. These are not instance variables, but local variables, therefore they are referred to as such (for example, βresourceβ).
- Using javascript_include_tag, on the other hand, makes a tag in HTML that references the JS file directly. This is fine if you just need to include some javascript that is not variable. They either should have been somewhere under your app / assets / file or your public / directory.
Files in the application / assets or publicly / can not access the variables in separate requests - they either do not compile (public /) or they are precompiled only once (application / assets).
Presumably this file is in your application / resource directory. To do this as partial, move it somewhere below the views / directory, place an underscore (_) in front of the name:
app/views/resources/_gallery_resource.js.erb
Benjamin cox
source share