2011-07-14 65 views
3

我試圖讓我的頭繞着這個問題,但我需要幫助!Rails 3 respond_to not responding

我正在使用rails 3和jquery,我有rails.js包含並使用gem'jquery-rails'來創建它 - 但仍然;當調用一個控制器使用$ .ajax({url:self.attr('href')});和respond_to:控制器內部的js,它只是使用html進行響應。

控制器:

respond_to :html, :js 

    # GET /customer/new 
    def new 
    @customer = Customer.new 

    respond_with(@customer) 
    end 

的application.js:

$.ajax({url: self.attr('href')}); 

起初我還以爲我這是用設置正確的頭一個問題,但在Firebug我可以看到所請求的X- - 設置爲XMLHttpRequest - 但仍然respond_with返回html而不是js。

我錯過了什麼?

回答

0

你的控制器看起來應該是這樣:

def new 
    @customer = Customer.new 
    respond_to do |format| 
    format.html # just renders like it normally would 
    format.js do 
     # Do whatever you need to do. 
     # By default it would render /customer/new.js.erb. 
     # 
     # If you're rendering some html from a view here, you'll want to 
     # tell rails not to render the layout by saying: render :layout => false 
    end 
    end 
end 
+2

爲什麼當即時通訊使用的respond_to頂部和respond_with這兩個應該照顧對我來說。我使用導軌3 –