Rails 3 - routing error when using act_as_taggable_on v.2.0.3

I am using act_as_taggable_on v.2.0.3 in Rails 3 to add tags to posts. I am adding a tag cloud as described here: https://github.com/mbleigh/acts-as-taggable-on , but I encountered an error: ActionController :: RoutingError in the columns # index: there are no route matches {: action => "tag" ,: id => "policy" ,: controller => "posts"}. My code is below:

PostHelper:

module PostsHelper include TagsHelper end 

Model Post:

 class Post < ActiveRecord::Base ... acts_as_taggable_on :tags end 

Postcontroller

 class PostController < ApplicationController ... def tag_cloud @tags = Post.tag_counts_on(:tags) end end 

View:

 <% 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 %> 

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 

What am I doing wrong? Thank you for your responses.

+6
ruby-on-rails acts-as-taggable-on
source share
2 answers

Hm, I think I understand. First, I edited route.rb this way:

 resources :posts do ... collection do get :tag end end 

Secondly, I added the Tag method to the PostController:

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

It works!

+7
source share

For the Rails4 approach, @muki_rails does not work. This is what I did:

In routes.rb :

 get 'tags/:tag' => 'articles#index', as: 'tag' 

Now I can do it in the view (I use slim ):

 - @article.tags.each do |tag| = link_to tag.name, tag_path(tag.name) 

Then in my ArticlesController , if the params[:tag] variable is set, I search for all relevant articles that match the given task.

  def index if params[:tag].present? @articles = Article.published.tagged_with(params[:tag]) else @articles = Article.published end end 
0
source share

All Articles