我有 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 ,因为此变量是空的(我不知道这是否是关于要点的错字)
答案 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 %>