Reputation: 2743
I'm pretty new with Ruby on Rails and I'm looking for the right way to include CSS/JS libraries for a special controller and method to avoid useless requests.
Right now I use this "ugly" way :
- # In a layout
- # IndustryManager::index -> We want load jquery toggles
- if action_name == "index" and controller_name == "industry_manager"
= javascript_include_tag "jquery-toggles/js/Toggles"
= javascript_include_tag "jquery-toggles/js/wrap"
= stylesheet_link_tag "jquery-toggles/css/toggles-full"
Upvotes: 2
Views: 132
Reputation: 96
A few options that are a bit cleaner than what you're doing:
1) Have one layout for each combination of css/js that you'll need, and specify the appropriate layout in the action. This only really makes sense if you have groups of actions that will use the same css/js, otherwise you'll have one layout per action which is a mess.
2) This is almost the same as what you're doing, but a bit more reusable-- set a variable in your action such as @include_toggles = true
and then include "jquery-toggles" only if that variable is true. You can then reuse that variable in other actions that need jquery-toggles, without having to check against the specific action and controller names.
3) If each action has its own css/js not shared with other actions, you could organize your files inside a directory that has the name of the action. Then you can include all files in that directory as described here https://stackoverflow.com/a/8902949/1786750, interpolating the name of the current action in the path, so that you get only the files needed for the current action. So for the index:
<%= javascript_include_tag Dir[Rails.root.join("public/javascripts/views/#{action_name}/*.js")].map { |s| s.sub(Rails.root.join("public/javascripts/").to_s, "") } %>
(That code is swiped from the answer above, I just added #{action_name}
to illustrate the concept.)
Hope that helps. There may be even better options but the above are at least simple and relatively clean.
Upvotes: 1