2

Hey,

I want to be able to put all of my pages' scripts in my application layout, and load each one for the appropriate page.

I came across this [question][1], and I thought I can put the scripts inside of partials and do it the same way that is indicated in the mentioned question, however I'm trying to find the best way for scripts.

Would it be something like :

yield :scripts if condition

or

unless condition
 content_for scripts do
 end
end 

or any other 'best' way ?


Note: Each page has its own script saved under the same name of the page.


Hamza Ouaghad
  • 589
  • 10
  • 23
  • Here's one solution: http://stackoverflow.com/questions/12828767/in-rails-how-to-check-if-javascript-file-exists-before-using-javascript-include – Damien Roche Jul 10 '14 at 17:29
  • 1
    There's many approaches that might work, but selecting the optimal one depends on a number of conditions. A conditional `yield` isn't a bad plan. – tadman Jul 10 '14 at 17:32
  • That's a possible solution, but as you can see I am searching for the simplest. **2 :** I don't need checking for existing, all files do exists, but I need to check for **existing with the same pages' name**. Any other ideas? @DamienRoche – Hamza Ouaghad Jul 10 '14 at 17:33
  • Can you provide the other approaches I seem to ignore? for I am searching for the simplest aka the best. @tadman – Hamza Ouaghad Jul 10 '14 at 17:34
  • what is an example of a controller/action and accompanying file name for your JS? Then I can advise how to automatically build that format. – Damien Roche Jul 10 '14 at 17:34
  • Pals/index has index.html.erb in the views, and index.js in javascript files dir. @DamienRoche – Hamza Ouaghad Jul 10 '14 at 17:35
  • So that `index.js` file is for *all* index pages, across *all* controllers? – Damien Roche Jul 10 '14 at 17:36
  • No, it is only for the `Pals` controller, and the other controllers have no `js` files, and my website turns around `pals` controller only, barely some 2 or 3 pages of other controllers with unique action names – Hamza Ouaghad Jul 10 '14 at 17:38
  • Well, you can use `= javascript_include_tag action_name`, but I wouldn't advise it. Better approach is to be more specific in naming structure (`pals_index.js`) and use `= javascript_include_tag [controller_name, action_name].join('_') if -- check if file exists` – Damien Roche Jul 10 '14 at 17:41
  • Provide it as an answer, I think it'll be working. Let me try meanwhile. – Hamza Ouaghad Jul 10 '14 at 17:44
  • I can't provide as an answer because I don't endorse it ;) it's a nasty hack and will not catch any number of edge cases. – Damien Roche Jul 10 '14 at 17:45
  • Btw, how the hell am I supposed to get the controller_name, and action name? XD? – Hamza Ouaghad Jul 10 '14 at 17:46
  • the `controller_name` and `action_name` should already be accessible from inside your views? They are helper methods. – Damien Roche Jul 10 '14 at 17:49

1 Answers1

1

Solved

I relied on the recommendations of @damien at the comments with the link he provided to come up with something like this:

In my helpers:

def javascript_exists?(script)
  script = "#{Rails.root}/app/assets/javascripts/#{[params[:controller], params[:action]].join('_')}.js"
  File.exists?(script) || File.exists?("#{script}.coffee") 
end

In my application layout:

  <%= javascript_include_tag [params[:controller], params[:action]].join('_'), :media => "all"  if javascript_exists?([params[:controller], params[:action]].join('_')) %>

So to each controller, and to each action in the controller, the appropriate .js file gets included if it exists.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Hamza Ouaghad
  • 589
  • 10
  • 23