在Rails2中使用Jquery,Jrails是必须的么?

在Rails2中使用Jquery时Jrails是不是必须要用的。在Stackoverflow上找到了一个相对全面的解答。

原文连接:http://stackoverflow.com/questions/5591289/is-jrails-a-must-for-rails-jquery

提问:

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.

  1. what is the difference between rails with and without jrails in term of developing a new rails app?
  2. what is the advantage of using jrails?
  3. is jrails a must for using jquery ui widgets in my rails app?
  4. any recommended tutorials of integrating jquery plugins in particular jquery ui selectable widget into a rails app?

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

  1. 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.

  2. See above; a Rails 2 app's server-side helpers that generate javascript will use jQuery instead of prototype.

  3. No. jRails only affects the javascript generated server-side.

  4. 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

Chris answered.

 

你可能感兴趣的:(jquery,prototype,Ruby,Rails,jrails)