开发者

Reverting back to using prototype

I've installed https://github.com/lleger/Rails-3-jQuery and it wo开发者_运维知识库rked fine, however, the rails helpers that I was using with prototype stopped working.

How can I get back to using prototype in order to use the rails helpers OR make the helpers work with jQuery (the last would be the ideal solution).

Thanks


Actually, the gem you are using is not good enough. You should be using jquery-rails. Install as follows

gem install jquery-rails

or add it to your Gemfile

gem 'jquery-rails'

and then do

rails g jquery:install

This not only removes the prototype.js, and downloads the jquery, but also (importantly!) downloads the adapted rails.js that works with jquery and will make sure that all standard rails-helpers will keep on working.


You need to load jQuery after prototype, then call jQuery.noConflict(), for example:

$j = jQuery.noConflict();

Then use $j for jQuery instead of $.

Or, still calling jQuery.noConflict(), you can use $ inside a function just for that, for example:

jQuery.noConflict();
//$ is prototype
(function($) {
  //$ is jQuery
})(jQuery);
//$ is prototype

Or say you're doing a document.ready handler, the short version would be:

jQuery.noConflict();
//$ is prototype
jQuery(function($) {
  //$ is jQuery, this runs when the DOM is ready
});
//$ is prototype


The Rails 3 helpers should work with jQuery too. (Stuff like link_to ..., :remote => true), the only thing to do is to load jQuery and the new rails.js.

The exception is RJS related helpers I guess.

Nick Craver's tip will work to make jQuery and prototype work together.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜