Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 3 - routing error when using acts_as_taggable_on v.2.0.3

I'm using acts_as_taggable_on v.2.0.3 in Rails 3 to add tags to posts. I add a tag cloud as described here: https://github.com/mbleigh/acts-as-taggable-on, but I'm encountered an error: ActionController::RoutingError in Posts#index: No route matches {:action=>"tag", :id=>"politics", :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? Thanks for your answers.

like image 291
sergodeeva Avatar asked Nov 13 '10 21:11

sergodeeva


1 Answers

Hmm, i think i understand. First, i edited routes.rb in a such way:

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

Second, i added method "Tag" in the PostController:

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

It works!

like image 176
sergodeeva Avatar answered Oct 06 '22 02:10

sergodeeva