hyperrjas
hyperrjas

Reputation: 10744

is it possible load a file.js.erb in <head></head>

I have a file.js.erb file in my views, but I need load this file in <head></head> layout.

Is it possible load these files in <head></head> section?

and if it's possible, How can I do it?

Thanks!

Upvotes: 0

Views: 510

Answers (1)

Shawn Balestracci
Shawn Balestracci

Reputation: 7530

in your layout:

<head>
  <script><%=render :file => "layouts/file.js"%></script>
</head>

If the file contains script tags then you wouldn't need them in the layout, and change the layouts in the path to wherever the file actually is.


If you don't want it on every page then you can use content_for and yield (as MyYorshiji suggests in the comments)

<head>
  <script><%= yield :extra_js %> </script>
</head>

In the view html.erb where you want this js to load

<%content_for :extra_js do %> 
  <%=render :file => "layouts/file.js"%>
<% end %>

http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#method-i-content_for

Upvotes: 2

Related Questions