Forum: Ruby on Rails Keeping javascript in the <head>

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Marlon M. (Guest)
on 2006-04-04 19:07
(Received via mailing list)
Given that Rails has built in ways to do almost everything else :) , is
there a way to keep view specific javascripts inside the head tag of a
layout without having to include that javascript on every page?
Charlie B. (Guest)
on 2006-04-04 20:06
(Received via mailing list)
make your include tag dynamic.  I can't remember the exact syntax but
you'll get the picture.

<%= include_javacripts, ****** #{@my_javascript} %>

in your view do something like this:
<% @my_javascript = 'view_specifiic_javascript.js' %>

Charlie B.
Rob B. (Guest)
on 2006-04-07 01:55
(Received via mailing list)
In your <head>, put:

  <script type="text/javascript"><%= @content_for_page_scripts %></

Then in your view:

<% content_for("page_scripts") do -%>
function item_added() {
   var item = $('items').lastChild;
   new Effect.Highlight(item, { startcolor:'rgb(255,153,255)',
endcolor:'rgb(200,200,200)' });
   $('form-submit-button').disabled = false;
   $('item-new-code').value = '';
   $('item-new-description').value = '';

function item_loading() {
   $('form-submit-button').disabled = true;'busy');
<% end -%>

Then the stuff will be in the header on the delivered page, but close
to where you need in in your .rhtml files.
content_for() works for other stuff, too!


Rob B.
+1 513-295-4739
Marlon M. (Guest)
on 2006-04-08 07:18
(Received via mailing list)
thanks for the great tip.  I had no idea about the @content_for command.
This topic is locked and can not be replied to.