How to display controller specific javascript in rails 3.1?

╄→尐↘猪︶ㄣ 提交于 2019-12-08 23:32:16

问题


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?


回答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.




回答2:


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




回答3:


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

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




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/7312465/how-to-display-controller-specific-javascript-in-rails-3-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!