2

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"
1
  • 1
    One common way to solve the too many requests would be to put all sources into one big file and minify that, as part of your build process. But that might not what you want. Commented Aug 31, 2014 at 22:10

1 Answer 1

1

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.

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

1 Comment

I hoped a magic answer, but your answer #2 and #3 do the job.

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.