Rails无尽的分页 - 未定义的局部变量

时间:2013-07-09 10:46:18

标签: jquery ruby-on-rails ruby pagination infinite-scroll

我有 UsersController ,在这个控制器中我正在加载用户的帖子,如下所示:

def welcome
  @postss = Post.page(params[:page]).per_page(10)
  render 'users/show'
end

然后在 users / show.html.haml

#posts
  = render 'posts/index', :posts => @postss
  = will_paginate @postss

users / show.js.erb

alert('a');
$('#posts').append('<%= j render('posts/show') %>');
<% if @postss.next_page %>
    alert('c');
  $('.pagination').replaceWith('<%= j will_paginate(@posts) %>');
<% else %>
    alert('c');
  $('.pagination').remove();
<% end %>

问题是,当我加载页面时,我在控制台中看到此错误消息:

ActionView::Template::Error (undefined local variable or method `post' for #<#<Class:0x007ff707e647d0>:0x007ff726648d98>):

但是我无法发现问题是什么 - 我在JS文件alert中添加了消息,但是没有弹出......我会很感激每一个建议。

非常感谢

修改 我跟着this topic。我的代码的区别在于,在块 index.html.erb 中我使用 @postss 而不是 @posts ,因为此变量是空的(我不知道这是否是关于要点的错字)

2 个答案:

答案 0 :(得分:0)

users / show.js.erb

$('.pagination').replaceWith('<%= j will_paginate(@posts) %>');
你在那里使用了错误的变量名!替换为 @postss

答案 1 :(得分:0)

users/show.js.erb

中试试
$('#posts').append("#{escape_javascript(render(:file => 'posts/show.html.haml'))}")
<% if @postss.next_page %>
  $('.pagination').replaceWith('<%= j will_paginate(@posts) %>');
<% else %>
  $('.pagination').remove();
<% end %>