我试图使用javascript / Jquery.ajax请求在rails上的ruby中使用部分模板更新页面。部分名为_promotions.html.erb我是一个使用javascript和JQuery的菜鸟,并且不太懂语法。从要更新的页面:
<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>
来自处理响应的rails控制器:
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 :(得分: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。