bassneck
bassneck

Reputation: 4043

Using ActsAsTaggableOn how do I make tags controller?

I have a taggable Game model. And now I want to make a controller to display objects by tags.

I'm able to select a tag by using @tag = ActsAsTaggableOn::Tags.find params[:id]. But how do I retrieve all the games associated with the tag? @tag.games obviously doesn't work.

Upvotes: 0

Views: 758

Answers (1)

Jeremy Raines
Jeremy Raines

Reputation: 1739

You probably don't need a TagsController.

To get what you're specifically asking for, you can use:

@tagged_games = Game.tagged_with :some_tag

If the tag is something passed into that controller action you can find it dynamically:

In routes.rb:

match 'games/tagged/:id' => 'games#tag', :as => :tags

In GamesController:

def tag
  @games = Game.tagged_with(params[:id])
  render :index
end

This would allow a user to go to /games/tagged/fps to get a listing of all games tagged as first person shooters, for example (assuming your index template is a general collection listing, anyway

Upvotes: 1

Related Questions