views:

1107

answers:

7

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?

+5  A: 

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.

kmorris511
This is a relief. Thanks.
Hola
+1  A: 

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.

Oliver N.
Ok, so the Rails ajax helpers can't be tweaked to work with Jquery.
Hola
+1  A: 

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.

Gabriel Hurley
Plus, Rails 3 will be much more JavaScript framework agnostic. Although it will still ship with Prototype.
John Topley
+11  A: 

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

seb
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
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
+8  A: 

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

Tony
Thanks for the snippet, man. That's great.
Hola
awesome solution, thx!
Darth
+2  A: 

The only problem with jrails is rjs testing. But this patch adds 5 of 8 missing test helpers.

Greg Dan
ajax form generators also seem to be a problem: http://stackoverflow.com/questions/363050/jrails-vs-prototype/363608#363608
Hola