phil swenson
phil swenson

Reputation: 8894

correct way to include javascript for a particular page in rails

It seems that the rails 3 way is to include all the javascript globally to the application. Am I correct on this?

What is the reasoning?

If I am not correct, what is the standard way to include page specific javascript?

thanks!

Upvotes: 1

Views: 202

Answers (4)

VenkatK
VenkatK

Reputation: 1305

To include specific javascript files into specific view pages only, you need to go with the following procedure.

i) In the assets folder create a separate folder eg. 'separate_view' , and inside put your specific js, and css files.

ii) In the application lay out write like following for 'separate_view'.
  <%= yield :separate_view %>

iii) In your target template write the following for 'separate_view'.
  <% content_for (:separate_view) do %>
    <%= javascript_include_tag "xxx.js" %>
    <%= stylesheet_link_tag "xxx.css" %>
  <%end%>

The above will go fine with your specific view files.

Upvotes: 0

Valery Kvon
Valery Kvon

Reputation: 4496

Many ways:

<%= javascript_tag "...javascript..." %>
* Generates <script type="text/javascript">...javascript...</script>

Or

<% content_for :script do %>
   ...javascript...
<% end %>
* This approach includes script into <%= yield :script %> defined in the template anywhere, for example inside the <head> dom:
<head>
  ...
  <% if content_for?(:script) %>
    <%= javascript_tag yield(:script) %>
  <% end %>

</head>

Or many many other ways.

Upvotes: 0

Erik Nomitch
Erik Nomitch

Reputation: 1665

By default, Rails 3 will include application.js which contains //= require_tree making it depend on on all other JS files in /app/assets/javascripts.

You can remove //= require_tree in application.js and <%= javascript_include_tag "application" %> in application.html.erb and include controller-specific JS by adding the following:

<%= javascript_include_tag params[:controller].split("/")[0])

Then, make JS files (app/assets/javascripts/CONTROLLER-NAME.js) for each controller containing:

  1. //= require application
  2. Your controller-specific JS

These will then depend on application.js but can have their own JS as well.

The .split method allows controllers like Devise to use their first name before the / as the name for the .js file so you only have to create devise.js instead of devise/sessions.js and others.

Upvotes: 1

Leo Correa
Leo Correa

Reputation: 19789

You could use the content_for tag to add page specific content to the head of the page.

In your

application.html.erb (your layout file) under your javascript includes you add a line that says

<%= content_for :js_include %>

then in any page you want to include a page specific javascript you just do

<% content_for :js_include do %>
  <%= javascript_include_tag "some_javascript_file" %>
<% end %>

I'm not sure if you have to inlcude the = sign on the javascript_include_tag (just make sure you put it as it is on the application.html.erb file but that's pretty much the gist of it. Using content_for tags

EDIT

Some helpful links

Railscasts Episode Layouts and Content For

Rails API content_for

Upvotes: 0

Related Questions