Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails and Page Specific Javascript

Ok, I realise this question has been asked many times but the answers never seems to address the issue/question I have with this.

I have a js file that I would like to include on specific pages only. There are many responses that suggest that I put files into folders and then in the manifest file reference only those folders - for example this Railscast (at about 06:20) talks about this.

However, I only have one application layout file (and I guess this could be the area I'm lacking in) - therefore this file points to the application.js manifest and therefore I can't see how I can include things conditionally.

It's a bit like this resource too - http://railsapps.github.io/rails-javascript-include-external.html - scroll down to the page-specific scripts sub heading and it repeats what the Railscast suggests. But nothing is mentioned of multiple application layout files.

If anyone can help me clarify what to do in this situation I would be most grateful.

I should perhaps point out that I'm using Rails 4.

like image 368
tommyd456 Avatar asked Sep 10 '13 16:09

tommyd456


People also ask

Where do I put JavaScript code in Rails 6?

The directory structure for JavaScript has changed to the app/javascript/packs/ folder. In that folder you will find the application. js file, which is just like the application. css file.

What is Javascript_include_tag?

javascript_include_tag(*sources) Link. Returns an HTML script tag for each of the sources provided. Sources may be paths to JavaScript files. Relative paths are assumed to be relative to public/javascripts , full paths are assumed to be relative to the document root.

What is Javascript_pack_tag?

The javascript_pack_tag takes care of making sure that it references compiled assets properly in development mode as well as in production mode similar to the assets pipeline.


2 Answers

You can use content_for in your views to "inject" content into the layout when said view is to be rendered. See: using-the-content-for-method

You'd need to do a few things to make this happen:

  1. Add the placeholder to yield the content in the layout. ex.

    <%= yield :js %>

  2. Add the block (to be yielded) to your view. ex:

    <%= content_for :js do %> <%= javascript_include_tag "my_script" %> <% end %>

  3. If you are using the asset pipeline in production and you want to reference a particular asset like a "my_script.js", in your production.rb or relevant environment config.you will need to precompile it using:

    config.assets.precompile=["my_script.js"]

like image 116
miked Avatar answered Oct 22 '22 08:10

miked


In application.html.erb, use this to add controller specific javascript instead of application specific javascript. Make sure you remove require_tree.

<%= javascript_include_tag params[:controller] %>

Read more on this topic on Rails Guide.

like image 24
Jason Kim Avatar answered Oct 22 '22 09:10

Jason Kim