2011-03-14 210 views
3

嗨 我剛剛遇到了一篇關於如何將Rails與jQuery相結合的文章。
http://blog.bernatfarrero.com/category/ror/
我按照指示,但我不能讓它工作(而不是使用Ajax軌道試圖處理請求的非JavaScript方式)。在我下載源代碼後,我發現問題出在哪裏。在public/javascripts的源文件中有一個文件jquery-rails.js,它與該行相連Ruby on Rails與jQuery

javascript_include_tag "jquery-rails.js" 

in application.html.erb。如果我在上面的行中更改該文件的名稱,則不再有效。所以我試圖改變從這裏下載的驅動文件的原始名稱:http://docs.jquery.com/Downloading_jQuery#Download_jQuery(許多不同的版本)到我在源代碼中找到的那個。當然我也把它放在同一個目錄中。而且它再次失敗......所以最後我比較了這些文件。我的意思是來自源頭的文件要短得多。那麼我是一個真正的JS初學者,所以我必須在這裏請求你的幫助。爲什麼它不起作用?如何使Rails與原始的jQuery驅動程序一起工作?
再見

下面你可以從源頭上看到 'jQuery的rails.js'

jQuery(function ($) { 
var csrf_token = $('meta[name=csrf-token]').attr('content'), 
    csrf_param = $('meta[name=csrf-param]').attr('content'); 

$.fn.extend({ 
    /** 
    * Triggers a custom event on an element and returns the event result 
    * this is used to get around not being able to ensure callbacks are placed 
    * at the end of the chain. 
    * 
    * TODO: deprecate with jQuery 1.4.2 release, in favor of subscribing to our 
    *  own events and placing ourselves at the end of the chain. 
    */ 
    triggerAndReturn: function (name, data) { 
     var event = new $.Event(name); 
     this.trigger(event, data); 

     return event.result !== false; 
    }, 

    /** 
    * Handles execution of remote calls firing overridable events along the way 
    */ 
    callRemote: function() { 
     var el  = this, 
      data = el.is('form') ? el.serializeArray() : [], 
      method = el.attr('method') || el.attr('data-method') || 'GET', 
      url  = el.attr('action') || el.attr('href'); 

     if (url === undefined) { 
      throw "No URL specified for remote call (action or href must be present)."; 
     } else { 
      if (el.triggerAndReturn('ajax:before')) { 
       $.ajax({ 
        url: url, 
        data: data, 
        dataType: 'script', 
        type: method.toUpperCase(), 
        beforeSend: function (xhr) { 
         el.trigger('ajax:loading', xhr); 
        }, 
        success: function (data, status, xhr) { 
         el.trigger('ajax:success', [data, status, xhr]); 
        }, 
        complete: function (xhr) { 
         el.trigger('ajax:complete', xhr); 
        }, 
        error: function (xhr, status, error) { 
         el.trigger('ajax:failure', [xhr, status, error]); 
        } 
       }); 
      } 

      el.trigger('ajax:after'); 
     } 
    } 
}); 

/** 
* confirmation handler 
*/ 
$('a[data-confirm],input[data-confirm]').live('click', function() { 
    var el = $(this); 
    if (el.triggerAndReturn('confirm')) { 
     if (!confirm(el.attr('data-confirm'))) { 
      return false; 
     } 
    } 
}); 


/** 
* remote handlers 
*/ 
$('form[data-remote]').live('submit', function (e) { 
    $(this).callRemote(); 
    e.preventDefault(); 
}); 

$('a[data-remote],input[data-remote]').live('click', function (e) { 
    $(this).callRemote(); 
    e.preventDefault(); 
}); 

$('a[data-method]:not([data-remote])').live('click', function (e){ 
    var link = $(this), 
     href = link.attr('href'), 
     method = link.attr('data-method'), 
     form = $('<form method="post" action="'+href+'"></form>'), 
     metadata_input = '<input name="_method" value="'+method+'" type="hidden" />'; 

    if (csrf_param != null && csrf_token != null) { 
     metadata_input += '<input name="'+csrf_param+'" value="'+csrf_token+'" type="hidden" />'; 
    } 

    form.hide() 
     .append(metadata_input) 
     .appendTo('body'); 

    e.preventDefault(); 
    form.submit(); 
}); 

/** 
* disable-with handlers 
*/ 
var disable_with_input_selector = 'input[data-disable-with]'; 
var disable_with_form_selector = 'form[data-remote]:has(' + disable_with_input_selector + ')'; 

$(disable_with_form_selector).live('ajax:before', function() { 
    $(this).find(disable_with_input_selector).each(function() { 
     var input = $(this); 
     input.data('enable-with', input.val()) 
      .attr('value', input.attr('data-disable-with')) 
      .attr('disabled', 'disabled'); 
    }); 
}); 

$(disable_with_form_selector).live('ajax:after', function() { 
    $(this).find(disable_with_input_selector).each(function() { 
     var input = $(this); 
     input.removeAttr('disabled') 
      .val(input.data('enable-with')); 
    }); 
}); 

});

回答

9

我假設你使用Rails 3,如果是使用jquery最簡單的方法是用jquery-rails寶石只需安裝它(添加gem 'jquery-rails')到您的Gemfile,運行bundle然後運行rails generate jquery:install並且可以使用jQuery的!

+0

那也行得通。非常好的方法謝謝! – gisek 2011-03-14 01:15:31

2

要在Rails的UJS獲取Ruby使用jQuery,你需要做以下的

  1. 在佈局包括jQuery的:從jQuery.com下載jQuery的到自己的公用目錄並鏈接到它與javascript_include_tag或使用Google's CDN,(這是我的偏好)

  2. 包括jQuery的Rails的司機:Rails的驅動程序使用jQuer y的功能來完成Rails特定的功能,這就是爲什麼你還需要jQuery。下載the jQuery Rails driver(你在你的問題中列出的文件)並用javascript_include_tag以同樣的方式鏈接到它。

+0

謝謝,它工作完美。 – gisek 2011-03-14 01:13:42