我的目标是在我的帖子页面中有一个标签云,这将允许用户通过单击标签来过滤帖子。但是,我遇到了一个未定义的方法“做了如下修改我的Post_Controller方法之后TOTAL_PAGES”错误:用will_paginate运行未定义的方法`total_pages'
class PostsController < ApplicationController
def index
if params[:tag]
@posts = Post.tagged_with(params[:tag])
else
@posts = Post.visible_to(current_user).where("posts.created_at > ?", 7.days.ago).paginate(page: params[:page], per_page: 10)
end
end
end
我试图使用行为-AS-加标签上的宝石,而这种逻辑将展示我用适当的标签帖子
问题发生在帖子/ index.html.erb景观:
<div class="row">
<div class="col-md-8">
<h1> Trending </h1>
<p class="lead"> Active posts this week </p>
<div id="tag_cloud">
Tag Cloud:
<% tag_cloud Post.tag_counts, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>
</div>
<%= render partial: 'posts/post', collection: @posts %>
<%= will_paginate @posts %>
</div>
<div class="col-md-4">
</div>
</div>
的will_paginate线将不会呈现该网页上的所有岗位的解决办法是摆脱<%= will_paginate @posts %>
并替换
@posts = Post.visible_to(current_user).where("posts.created_at > ?", 7.days.ago).paginate(page: params[:page], per_page: 10)
与@posts = Post.all
。但是,这给了我一整页的帖子,这很丑陋。有谁知道我为什么遇到未定义的方法'total_pages'错误?
有几个相同的SO这个问题,花一点时间来通过他们去 - 相信你会找到答案 – 2014-09-21 20:50:04
是当params [:tag]被设置或没有发生错误? – 2014-09-21 20:55:43
@FrederickCheung该页面可以正常加载帖子和云标签。然而,只有当我点击一个标签来过滤帖子时,错误才会发生。 params [:tag]为false时发生错误。 – ShaunK 2014-09-21 20:58:16