2015-11-30 54 views
0

我有打Rails控制器Ajax請求:Rails的渲染並不使我的新觀點

class MyController < ApplicationController 

    respond_to :json, :html 

    def my_method 
    #logic happens here to produce an instance variable 
    render template: 'other/template', layout: 'application' 
    end 
end 

邏輯重創,鐵軌控制檯顯示:

Rendered other/template.html.haml within layouts/application (34.8ms) 

然而,瀏覽器不更新DOM。我錯過了什麼?我怎樣才能使這個工作?

+1

您的AJAX代碼必須處理響應並將其插入到DOM中。 – sevenseacat

回答

0

您需要編寫my_method.js.erb文件,而不是像其他/ _template.html.erb一樣創建部分模板。 在my_method.js.erb寫下面的代碼:

$("#ID").html("<%= escape_javascript render(:partial => 'other/template')%>"); 

class MyController < ApplicationController 

    respond_to :json, :html 

    def my_method 
    #logic happens here to produce an instance variable 
    end 
end 

請不是#ID是要替換部分結果的DOM元素。