9

I have my assets folder structure like this

assets
  javascripts
    products
      --product.js
      --productValidate.js
    store
      --store.js

I want the project.js and projectValidate.js to be added in my application.js as a part of asset pipe-lining only when actions in product controller is called and store.js when actions in store controller is called. How can i achieve this in rails 3.1?

5 Answers 5

4

As Rahul already mentioned, application.js is precompiled and the same for every action. So it does not depend on a particular controller. Application.js should contain the javascript you need for all (or most) of your actions.

However, you may expand your application layout with nested layouts. Let us assume the following structure:

... app/view/layouts/application.html.erb ...

<html>
<head>
  <%= javascript_include_tag 'application' %>
  <%= yield :javascripts %>
  <%= stylesheet_link_tag 'application' %>
  <%= yield :stylesheets %>
</head>
<body>
  <%= yield %>
</body>
</html>

and a:

... app/view/layouts/products.html.erb ...

<% content_for :stylesheets do %>
  <%= stylesheet_include_tag 'products' %>
<% end %>
<% content_for :javascripts do %>
  <%= javascript_include_tag 'products' %>
<% end %>
<%= render :template => 'layouts/application' %>

So you just have to add/require your stylesheets and javascripts in the products-files. Notice, all code here should be read as pseudo-code, I did not test it.

Information taken from the "official" Render-Guide.

Sign up to request clarification or add additional context in comments.

2 Comments

This is what I use too. But I put the javascript tags to right before <html> tag.
You should put javascript tags before the closing </body> tag if you want to be a good citizen.
1

As far as I know assets pipilene is something that should be precompiled. So... conceptually it should take all files at once and return just one copiled file, and it is good for caching.

You can store them somewhere out od assets (in puplic, as older Rails do, for example) and include it in depending to current controller and action

2 Comments

I just saw railscast 279,in which Ryan mentions that v can specify which folders v wanna add with the application.js file by using require_directory instead of require_tree in the sprockets. I wanna know is there similar ways for adding controller specific js.
Ryan mentioned WHICH FOLDERS you want to COMPILE it is not in the runtime :)
1

I like the approach mentioned in the answer to this question:

Rails 3.1 asset pipeline: how to load controller-specific scripts?

Comments

1

This is what I am working on now to bridge the gap between Rails 3.1 assets pipeline and Jammit.

I have not provided any documentation as it I consider it under development yet. But this is what it basically should allow you to do:

# config/initializers/pakunok.rb
# Define the dependencies between Rails assets:
require 'pakunok'
Pakunok::Pakunok.current.configure do
  asset('products/product.js').needs('products/productValidate.js')
end

And then, in your layout, you only need to do a single include. All the dependent files will be included automatically:

# app/views/layouts/application.html.erb
<%= include_javascripts %>

Please see the specs to learn what the gem can do. Note: it is still under development and the gem itself will be split into multiple ones later.

For any particular use-cases that you want to be supported, please submit issues and I promise to work hard to implement those when I'll have time :)

Comments

0

The rails documentation recomends this way: http://guides.rubyonrails.org/asset_pipeline.html#controller-specific-assets

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.