Is jRails a must for Rails + jQuery?

为君一笑 提交于 2019-12-05 18:38:54

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.

Also, use Rails 3, not Rails 2.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!