我已經安裝了https://github.com/lleger/Rails-3-jQuery,它工作正常,但是,與原型一起使用的rails helpers停止工作。恢復使用原型
我怎樣才能回到使用原型,以使用rails helper或使助手工作與jQuery(最後將是理想的解決方案)。
感謝
我已經安裝了https://github.com/lleger/Rails-3-jQuery,它工作正常,但是,與原型一起使用的rails helpers停止工作。恢復使用原型
我怎樣才能回到使用原型,以使用rails helper或使助手工作與jQuery(最後將是理想的解決方案)。
感謝
其實,你使用的寶石不夠好。你應該使用jquery-rails
。 安裝如下
gem install jquery-rails
或將其添加到您的Gemfile
gem 'jquery-rails'
,然後做
rails g jquery:install
這不僅消除了prototype.js中,並下載jQuery的,但也(重要的是!)下載適用於jQuery的rails.js,並確保所有標準的rails-helpers將繼續工作。
你需要後原型加載jQuery的,然後調用jQuery.noConflict()
,例如:
$j = jQuery.noConflict();
然後使用$j
的jQuery,而不是$
。
或者說,她一直在叫jQuery.noConflict()
,您可以使用$
函數裏面只是,例如:
jQuery.noConflict();
//$ is prototype
(function($) {
//$ is jQuery
})(jQuery);
//$ is prototype
或者說你做一個document.ready
處理,短版本是:
jQuery.noConflict();
//$ is prototype
jQuery(function($) {
//$ is jQuery, this runs when the DOM is ready
});
//$ is prototype
Rails 3助手也應該使用jQuery。 (東西像link_to ..., :remote => true
),唯一要做的就是加載jQuery和新的rails.js。
我猜想例外是RJS相關的助手。
Nick Craver的技巧將使jQuery和原型一起工作。