原文连接:http://stackoverflow.com/questions/5591289/is-jrails-a-must-for-rails-jquery
i'm new to rails (using rails 2.3.10) and would like to use jquery in particular jquery ui. the project is a brand new project and i'm planning to use some widgets from jquery ui like datapicker and selectable, etc. i having been hearing that jrails can be installed to replace prototype in rails2 and i have the following questions.
thanks batterhead
?
user661684 asked
?
回答:
?
First up, if you're writing a brand new project, I would recommend considering using Rails 3. Among other things, it actually abstracts away the default JS framework used by Rails, so it's much easier to remove Prototype entirely from Rails 3.
As far as using jQuery UI, nothing is stopping you from including jQuery and jQuery UI in the project. Prototype and jQuery can live together in your app just fine (though you need to set a flag so that jQuery doesn't override prototype's "$" function; how to do that is another question).
The only reason jRails was written is because of the RJS features of Rails 2 -- the ability to write javascript templates on the server. Prototype is intrinsically linked to the RJS functionality of Rails, as all the helper methods for RJS would translate to Prototype calls. In Rails 3, that functionality has been deprecated in favor of using more unobtrusive javascript (UJS) techniques, so all the JS code stays on the client side. Thus, the reliance on Prototype is no longer an issue in Rails 3.
?
TL;DR
Without jRails, all javascript generated server-side will
translate in to prototype javascript calls on the client-side -- RJS
template helper methods, link_to :method => :delete
calls, etc.
With jRails, all those server-side methods will translate in to jQuery calls client-side instead.
See above; a Rails 2 app's server-side helpers that generate javascript will use jQuery instead of prototype.
No. jRails only affects the javascript generated server-side.
You can include and use jQuery in your app in the same way you would include any other javascript resource in Rails 2. Once jQUery is included, its no different to using it on plain HTML, so look at the jQuery UI tutorials on how to use the datepicker or selectable
?