I saw comments in a previous question saying that it is best to use Prototype with Rails. However, my own experience is that Jquery is a superior Javascript library. Being new to Rails, I have not yet investigated how to use Jquery with Rails but I assumed this would work. Is it correct that this may be a problematic combination - especially in relation to Ajax - and that I may need to use Prototype instead?
7 Answers
I've worked on all my projects since 3 years with rails and (exclusively) jquery. Never really encountered any (serious) problems so far.
There is a plugin called jrails, which acts as a drop-in replacement for prototype.
http://github.com/aaronchi/jrails/tree/master
Update: with it you can get all of the same default Rails helpers for javascript functionality using

- 3,274
- 4
- 22
- 16
-
Interesting plugin. Thanks for the link. Have to investigate that further. I was actually wondering how to do replace_html with Jquery so this is great. – Hola Jun 04 '09 at 17:47
-
I'll add that unless you are using anything with explicit use of prototype (Javascript strings vs rails helpers like remote_form_for), it's easy. On one of my projects, I use an admin backend plugin which NEEDS prototype so I had to do some stuff to make sure it worked, but other than that, no problems. – Daniel Huckstep Nov 12 '09 at 21:24
I use both jQuery and Prototype with rails. jQuery for DOM manipulation and thickbox (my favorite light box plugin), but i use prototype for AJAX right now. no particular reason, just haven't wanted to use the jrails plugin yet. im sure i will do this in the future. if you use both, this should be in your head tag:
<%= javascript_include_tag 'prototype' %>
<%= javascript_include_tag 'jquery' %>
<script type="text/javascript">
var $j = jQuery.noConflict();
</script>
Then use jQuery with $j

- 18,776
- 31
- 129
- 193
-
1I've just noticed that it's important to include jQuery *last* so remember that. – madh Nov 24 '10 at 18:30
I use jQuery and Rails on the job in a production environment and have only nice things to say. We use AJAX too. The only problem I can think of is the jQuery.noConflict() call that's necessary if you're using jQuery and prototype together.

- 16,392
- 7
- 28
- 29

- 6,198
- 3
- 33
- 53
-
ajax form generators also seem to be a problem: http://stackoverflow.com/questions/363050/jrails-vs-prototype/363608#363608 – Hola Jun 04 '09 at 21:48
It won't be problematic to use jquery with rails at all, it's just not the natively supported JavaScript library. You don't actually have to use any of the JavaScript builtins in rails, and the (by default) RESTful structure of your application should make AJAX simple.
With prototype and scriptaculous, alot of the AJAX work has already been done for you. With jquery, you'll just be writing some more JavaScript yourself.

- 2,496
- 19
- 20
Rails was built to work with Prototype for AJAX et. al., but there's no reason you have to use Prototype. I'm a jQuery fan myself, and you can definitely use it with Rails.
You can still hook into a lot of the AJAX work that's been done in Rail with jQuery, not a problem.
And, as kmorris said, if you're using jQuery and any other javascript library on the same site, be sure to add the jQuery.noConflict()
line in or else VERY bad things will happen.

- 39,690
- 13
- 62
- 88
-
Plus, Rails 3 will be much more JavaScript framework agnostic. Although it will still ship with Prototype. – John Topley Jun 04 '09 at 18:03
Try this: https://github.com/xpepermint/js_erb
Javascript becomes part of app, i18n and js templating become available and more.

- 35,055
- 30
- 109
- 163