5

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.

2
  • 1
    I'll suggest you to follow good practice before breaking it. You may think you'll have some performance gain by doing that, but the result is probably not. Even if you can gain some, "premature optimization is the root of all evil". Do it first, make all working, and optimize later.CommentedSep 10, 2013 at 16:39
  • 1
    I'm not breaking it - I don't understand it!!! If my application layout file is referencing the application.js manifest how can I make things conditional???
    – tommyd456
    CommentedSep 10, 2013 at 16:41

2 Answers 2

5

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"]

3
  • yes but then I would have to create the same code twice if I wanted to call upon the js in two pages. Therefore I decided to put the js into a partial and then call that same partial in two different places
    – tommyd456
    CommentedSep 10, 2013 at 19:16
  • You'd only have to put the content_for block in multiple places...similarly to rendering your partial. If you want to change the script you're referencing, it sounds like you'll have to create another partial whereas with content_for, you'd just change the file name. Whatever works for you.
    – miked
    CommentedSep 10, 2013 at 19:26
  • The js itself is in your js file (my_script.js). So, if you have my_view.html.erb that needs my_script.js, as your topic suggests, you would add the content_for block containing my_script.js in my_view.html.erb. Doing so will ensure that my_script.js will not be included for any rendered page except my_view.html.erb. Is this not what you're asking?
    – miked
    CommentedSep 10, 2013 at 20:19
4

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.

0

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.