简体   繁体   中英

How to display controller specific javascript in rails 3.1?

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?

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 .

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

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

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

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM