Search code examples
ruby-on-railsrubyruby-on-rails-plugins

How to find and display a group of objects with a specific tag using acts as taggable (redux)?


I want to display a list of articles in my tag's show view. I'm using the acts as taggable redux plugin in a Rails 2.3.2 and SQLite3 app. I've got as far as the basic example takes me, assigning tags and then displaying a list of them with the article.

Now I want to display a list of articles belonging to a tag but get the following error:

undefined method `article' for #<Tag id: 1, name: "various", taggings_count: 1>

Models

/article.rb

class Article < ActiveRecord::Base  
  acts_as_taggable
end

/user.rb

class Tag < ActiveRecord::Base  
  acts_as_tagger
end  

Controller

/tags_contoller.rb

def show
  @tag = Tag.find(params[:id])
  @articles = @tag.articles
end  

View

/tags/show.html.erb

<% for article in @articles %>  
   ...  
<% end %>  

Here is a link to the migration file.

Thanks very much.


Solution

  • Many thanks to Geemus, author of acts as taggable redux for explaining this to me.

    To display a list of articles with a specific tag I just needed to change the tags_controller from:

    /tags_contoller.rb

    def show
      @tag = Tag.find(params[:id])
      @articles = @tag.articles
    end
    

    to:

    Controller /tags_contoller.rb

    def show
      @tag = Tag.find(params[:id])
      @articles = @tag.tagged
    end