What is the scope of the Ruby code in the js.erb file and how it can access controller variables

I have a js.erb file in which I want to use object_path. It is included in the following:

<%= javascript_include_tag "resources/gallery_resource" %> 

This works fine until the resource path is used independently, however I need to use the variable defined in the controller (@resource) inside the resource path:

 element.image = "<%= asset_path 'resources/galleries/' +@resource.uid +'/'+index+'.jpg' %>" 

The @resource problem is not defined. So, what is the volume of this Ruby code if it does not have access to the controller? And how do I make a variable available here?

I understand how to make a variable available for Javascript, but I want to make it available for embedded Ruby.

0
javascript ruby ruby-on-rails ruby-on-rails-3 erb
source share
2 answers

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 
+1
source share

For the record, my solution to this problem was to capture the base path using a token:

 var base_path = "<%= asset_path 'resources/galleries/{{X}}.jpg' %>"; 

Then replace the token using the controller variable available with the excellent gon gem

 var imagePathFragment = gon.resource.uid+'/'+(index+1); element.image = base_path.replace('{{X}}', imagePathFragment); 
+1
source share

All Articles