我有一个index.htm.erb文件,其中有一个渲染模板文件。从js文件访问轨道控制器变量
<div id="product"> <%= render :template => "shop/products" %> </div>
在其控制器
我
def index
@products = Product.paginate :page => params[:page], :per_page => 6
end
我有一个js文件,它是在我的index.html文件的底部,其上有个剧本得到will_paginate网址。
$(window).scroll(function(){
if($(this).scrollTop() > (($(document).height() - $(this).height()) - 50)){
var link = $('.pagination .next_page').attr('href');
$.getScript(link);
}
});
此getScript调用与index.html.erb具有相同名称的js文件。所以它的index.js.erb。 里面的js.erb文件我有一个
console.log("products = ", @products);
我在尝试获取实例变量的错误。 500内部服务器错误 我无权访问@products变量。 我需要做些什么来访问这个变量。
更新:
我使用的是相同的教程从http://railscasts.com/episodes/114-endless-page-revised?autoplay=true
唯一的区别是我用我的index.html.erb 内模板所以我还是不明白如何在本教程他们可以访问的实例变量,但我不
感谢Mathias,但我使用的是来自railscast的教程,他们能够访问实例变量。唯一的区别是我正在使用模板。在我原来的帖子中查看我的更新评论。 – Chapsterj
是的,Mathias你的回答是不正确的,因为Rails有能力使用Ruby来动态构建JS,然后将其提供给客户端。事情已经改变了> rails 3.1,但在3.0这仍然是可能的 – light24bulbs
谢谢指出。我会编辑我的答案。 – Mathias