# Unobtrusive javascript -\> attaching a .js.erb page

**URL:** <https://www.ruby-forum.com/t/unobtrusive-javascript-attaching-a-js-erb-page/206759>\
**Category:** Rails\
**Created:** [May 9, 2011, 3:30pm UTC](https://www.ruby-forum.com/t/unobtrusive-javascript-attaching-a-js-erb-page/206759 "2011-05-09T15:30:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [May 9, 2011, 3:30pm UTC](https://www.ruby-forum.com/t/unobtrusive-javascript-attaching-a-js-erb-page/206759/1 "2011-05-09T15:30:33Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![spacechurro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d2c977/32.png) [@spacechurro](https://www.ruby-forum.com/u/spacechurro)\
**Post date:** [May 11, 2011, 6:44pm UTC](https://www.ruby-forum.com/t/unobtrusive-javascript-attaching-a-js-erb-page/206759/2 "2011-05-11T18:44:43Z")

</div>

On 9 May 2011 14:30, Sergio R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](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
