Rails 3 - 使用acts_as_taggable_on v.2.0.3时发生路由错误

ser*_*eva 6 ruby-on-rails acts-as-taggable-on

我在Rails 3中使用acts_as_taggable_on v.2.0.3来为帖子添加标签.我添加了一个标签云,如下所述:https://github.com/mbleigh/acts-as-taggable-on,但我遇到了一个错误: Posts中的ActionController :: RoutingError #index:没有路由匹配{:action =>"tag",:id =>"politics",:controller =>"posts"}.我的代码如下:

PostHelper:

module PostsHelper
  include TagsHelper
end
Run Code Online (Sandbox Code Playgroud)

模特岗位:

class Post < ActiveRecord::Base
  ...
  acts_as_taggable_on :tags
end
Run Code Online (Sandbox Code Playgroud)

PostController中

class PostController < ApplicationController
  ...
  def tag_cloud
    @tags = Post.tag_counts_on(:tags)
  end
end
Run Code Online (Sandbox Code Playgroud)

视图:

<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
  <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

routes.rb中:

Blog::Application.routes.draw do
  root :to => "posts#index"
  resources :posts do
    member do
      post :notify_friend
    end
    collection do
      get :search
    end
    resources :comments
  end
  resources :users
  resource :session
  match '/login' => "sessions#new", :as => "login"
  match '/logout' => "sessions#destroy", :as => "logout"
end
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?谢谢你的回答.

ser*_*eva 7

嗯,我想我明白了.首先,我以这样的方式编辑routes.rb:

resources :posts do
  ...
  collection do
    get :tag
  end
end
Run Code Online (Sandbox Code Playgroud)

其次,我在PostController中添加了方法"Tag":

  def tag
    @posts = Post.tagged_with(params[:id])
    @tags = Post.tag_counts_on(:tags)
    render :action => 'index'
  end
Run Code Online (Sandbox Code Playgroud)

有用!