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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
50c1f2f516dfe5b6256ec9f62a949ba6?d=identicon&s=25 Marlon Moyer (Guest)
on 2006-04-04 17: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?
89d967359903c639d31e4cad4569f537?d=identicon&s=25 Charlie Bowman (Guest)
on 2006-04-04 18: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 Bowman
recentrambles.com
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2006-04-06 23:55
(Received via mailing list)
In your <head>, put:

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


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)' });
   Element.hide('busy');
   $('form-submit-button').disabled = false;
   $('item-new-code').value = '';
   $('item-new-description').value = '';
   Field.focus('item-new-code');
}

function item_loading() {
   $('form-submit-button').disabled = true;
   Element.show('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


Rob Biedenharn		http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
+1 513-295-4739
50c1f2f516dfe5b6256ec9f62a949ba6?d=identicon&s=25 Marlon Moyer (Guest)
on 2006-04-08 05: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.