Jquery Rails无止境分页-未定义的局部变量
我有UsersController,在这个控制器中,我加载用户的帖子,如下所示:Jquery Rails无止境分页-未定义的局部变量,jquery,ruby-on-rails,ruby,pagination,infinite-scroll,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
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 %>
$('.pagination').replaceWith('<%= j will_paginate(@posts) %>');
警报('a');
$('#posts')。追加('');
警报(“c”);
$('.pagination')。替换为('';
警报(“c”);
$('.pagination').remove();
问题是,当我加载页面时,我在控制台中看到以下错误消息:
ActionView::Template::Error (undefined local variable or method `post' for #<#<Class:0x007ff707e647d0>:0x007ff726648d98>):
ActionView::Template::Error(未定义的局部变量或#的“post”方法):
但我无法检测出问题所在-我将消息放入JS文件alert
中,但没有弹出。。。如果你能给我提些建议,我将不胜感激
非常感谢
编辑:
我跟着。我的代码的不同之处在于,在块index.html.erb中,我使用@postss而不是@posts,因为用户/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 %>
$('.pagination').replaceWith('<%= j will_paginate(@posts) %>');
$('.pagination')。替换为('';
您使用了错误的变量名!替换为@postss?在
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 %>
$(“#posts”).append(“#{escape_javascript(render(:file=>'posts/show.html.haml'))}”)
$('.pagination')。替换为('';
$('.pagination').remove();
错误的实例变量…应为@postss,如您在控制器提供中所述。请控制器显示方法Hi Joe,您的意思是posts/show
或users/show
?谢谢,我试过了,但还是有同样的错误。向下滚动时,我只看到“获取数据…”,在日志未定义的局部变量或方法'post'中,…
日志中的错误也指向这一行:$('#posts')。append('')
这样您就不会将“post”变量传递给正在使用它的show模板