我正在尝试使用javascript/Jquery.ajax请求来使用ruby on rails中的部分模板更新页面。这个部分被称为_promotions.html.erbI,我是一个有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预处理)。我哪里错了?
发布于 2013-04-24 15:58:38
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。
https://stackoverflow.com/questions/16183079
复制相似问题