Forum: Ruby on Rails Where to put Javascript?

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.
05748a61a1b05130ec508434301aecf1?d=identicon&s=25 Lee (Guest)
on 2007-01-18 10:20
(Received via mailing list)
I want to add some simple JS code to my application that registers an
'onclick' event for a form field and calls a function to change various
DOM elements.

Where is the best place to put this?

In the layout for the view? In application.js?

83ca41657a99b65d99889abe712ba5e2?d=identicon&s=25 Jason Roelofs (Guest)
on 2007-01-18 14:55
(Received via mailing list)
As I'm sure you're aware, Javascript can go basically anywhere, and
there is
no set place to put said JS. If the script is to work and be available
every page, either put it in your layout, or if it's complicated (or
are multiple layouts), then yeah, put it in the public/javascripts
If it's simple, small functions and you want to keep the source clean,
you can do something like this:

in layout:

   <script ...>
      <%= yield :javascript %>

and in your rhtml:

<% content_for(:javascript) do %>
  put javascript stuff here
<% end %>

Hope that helps.

This topic is locked and can not be replied to.