【问题标题】:Cannot sort posts by tag using acts as taggable on无法使用作为可标记的标签按标签对帖子进行排序
【发布时间】:2014-01-17 11:37:36
【问题描述】:

我的posts_controller中有一个方法:

  def tag
    @posts = Post.all.tagged_with(params[:id]).page(params[:page]).per(10)
    render :action => 'index'
  end

我的路线文件:

  resources :posts do
    collection do
      get :top
      get :revelance
      get :tag
    end
  end

我输出标签的视图文件:<%=raw post.tag_list.map {|t| link_to t, tag_posts_path(t), class: "tags" }.join (' ') %>

问题是:当我点击标签时,我会到达 url: */posts/tag.Sometag 并且页面是空的。 我想我需要更改我的路线文件.. 但我不知道该怎么做

【问题讨论】:

  • 您想获得一个包含所选标签的帖子列表的页面吗?使用 uri:/posts/tag/:tag_name ? (你也可以看演员railscasts.com/episodes/382-tagging 。关于标签有很多东西。)

标签: ruby-on-rails routes rails-routing acts-as-taggable-on


【解决方案1】:

你其实并不需要这个标记方法,你可以直接在index动作中做作业:

def index
  @posts = if params[:tag]
    Post.all.tagged_with(params[:tag]).page(params[:page]).per(10)
  else
    Post.all.page(params[:page]).per(10)
  end
end

然后你可以像这样定义你的路线:

get 'tags/:tag', to: 'posts#index', as: :tag

并根据您的观点进行调整:

<%= raw post.tag_list.map {|t| link_to t, tag_path(t), class: "tags" }.join (' ') %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-06
    • 2021-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多