Forum: Ruby on Rails Where do I put my JavaScript?

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.
05748a61a1b05130ec508434301aecf1?d=identicon&s=25 Lee (Guest)
on 2007-01-19 18:04
(Received via mailing list)
I'd like to include some simple JavaScript in my application that
registers an event handler (onclick) on a particular field in one of my
views and then calls a function to do some processing.

Where should I place the event handler and function code? In
application.js? Or in the layout that gets rendered with the view?

Thanks.
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-01-19 18:23
(Received via mailing list)
> I'd like to include some simple JavaScript in my application that
> registers an event handler (onclick) on a particular field in one of my
> views and then calls a function to do some processing.
>
> Where should I place the event handler and function code? In
> application.js? Or in the layout that gets rendered with the view?

Kind of depends on where that view gets called from.  If it's just in
one
spot or if it's a partial that gets included all over.

Also depends on whether your event handler is generic enough that it
could
be reused elsewhere.

Assuming it's one view and quite specific, I would put the code in:

public/javascripts/controller_name/action_name.js

(replacing controller_name and action_name with the right values)

Then include that in your view..

The nice thing about this is that it makes it (somewhat) easier to see
what javascript (or CSS or images) go with what controllers/actions...
This topic is locked and can not be replied to.