Unobtrusive javascript -> attaching a .js.erb page

i saw on one of the railscasts that using unobtrusive javascript, you
attach your javascript to your page like so:

if your view is:

index.html.erb

you could add a page named:

index.js.erb

and that javascript would be included on the load for that page.

for some reason, it is not working for me…

is there something i need to do to trigger this behavior?

thanks!

On 9 May 2011 14:30, Sergio R. [email protected] wrote:

and that javascript would be included on the load for that page.

for some reason, it is not working for me…

is there something i need to do to trigger this behavior?

There is no such behaviour in Rails; I think you might have
misunderstood the screencast.

Assuming you are talking about this:

http://asciicasts.com/episodes/205-unobtrusive-javascript

then the ‘index.js.erb’ is rendered when your browser makes an AJAX
request to the ‘index’ action (in the same way that ‘index.html.erb’
is rendered when your browser makes an ordinary request to the index
action).

These are two completely separate requests. index.js.erb is not
being inserted into a script tag in index.html.erb.

If you want that kind of thing to happen, you’ll need to write a
custom view helper (and probably choose a more appropriate path for
your automatically-included JavaScript files, so that they don’t
conflict with the standard behaviour described above).

Chris