2013-04-24 149 views
0

我是个局部模板尝试使用JavaScript/Jquery.ajax请求在Ruby on Rails的部分模板更新页面更新页面。该部分被称为_promotions.html.erb我是一个与JavaScript和JQuery的noob,并不理解语法。从页面进行更新:Jquery.ajax请求与红宝石上轨

<div id="promotions"></div> 
<script type="text/javascript"> 
    jQuery.ajax({ 
    url: "/promotions", // it routes to the following def 
    type: "GET", 
    dataType: "html" 
    success: $(function(data){$('#promotions').write(data)}) 
    }); 
</script> 

从轨控制器处理响应:

def promotions 
    @items = Item.search("", 8) #the partial needs this variable during preprocessing 
    respond_to do |format| 
    format.html { render :partial => "promotions", :layout => false, } 
    end 
end 

我想渲染_promotions.html.erb到页面中。最终,我希望能够定期重新渲染那个div的内容而不刷新整个页面(因此jquery调用而不是通过rails预处理)。我哪里错了?

回答

0
jQuery.ajax({ 
    url: "/promotions", 
    type: "GET", 
    dataType: "html", // You missed a comma. 
    success: function(data){$('#promotions').html(data)} 
}); 

没有jQuery .write()方法。
另外,不要在$(...)包装function(data){$('#promotions').html(data)}像你一样。

$('#promotions')返回ID 优惠的DOM元素,但$(function() {})仅用于bind a function which runs when the document is ready