# Unobtrusive Javascript in Rails

**URL:** <https://www.ruby-forum.com/t/unobtrusive-javascript-in-rails/155565>\
**Category:** Rails\
**Created:** [December 13, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/unobtrusive-javascript-in-rails/155565 "2008-12-13T17:05:17Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![daz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/87869e/32.png) [@daz](https://www.ruby-forum.com/u/daz)\
**Post date:** [December 13, 2008, 5:05pm UTC](https://www.ruby-forum.com/t/unobtrusive-javascript-in-rails/155565/1 "2008-12-13T17:05:17Z")

</div>

Hi everybody,

I love using rails, but one of my major problems with it has always  
been how the helper methods create some really ugly inline javascript.

I’ve recently found jQuery after watching Ryans brilliant Railscast  
about it ([#136 jQuery - RailsCasts](http://railscasts.com/episodes/136-jquery)).

I think that jQuery is a great way to make Rails unobtrusive and it  
has a really easy to pick up syntax.

The main problem is though that you can’t use any of those nice helper  
methods anymore!

Is anybody interested in helping me to get a project going to try and  
set some best practice, code snippets to do common tasks or even  
jQuery or Rails plugins that would help make unobtrusive JS just as  
easy as the default JS in Rails? If anybody has already done any of  
this then please let me know. Here is a wishlist to get started:

- Find an unobtrusive way of deleting things in a RESTful way
- Faking the PUT and DELETE verbs unobtrusively
- Adding an authenticity token to forms unobtrusively (the form\_for  
helper adds a bit of inline javascript)
- Which jQuery plugins are essential? Should they all be packaged up  
as a Rails plugin that would work similar to \<%  
javascript\_include\_tag :defaults %\>
- Easy drag and drop that plays nicely with the controller
- Sortable lists and sortable nested lists
- Inline editing
- Are there any settings that are needed (such as when Ryan sets this  
up in the railscast)  
jQuery.ajaxSetup({  
‘beforeSend’: function(xhr) {xhr.setRequestHeader(“Accept”, “text/  
javascript”)}  
})  
or the fact that you need something like the following before you can  
use Ajax with authenticity tokens:  
\<%= javascript\_tag “window.\_token = ‘#{form\_authenticity\_token}’” if  
protect\_against\_forgery? %\>

Hope this gets some discussion going … I don’t intend for this to  
become a discussion about whether or not UJS is worth it or not. If  
you are happy with the current inline stuff then please continue to  
use it, but I know that a lot of people want UJS and this is an  
attempt to make it as easy as possible for them to do that.

Looking forward to any thoughts…

cheers,

DAZ
