How to find and display a group of objects with a specific tag using acts as taggable (redux)?
-
06-07-2019 - |
Question
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 3
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
OTHER TIPS
I believe the plugin is not the one to blame here, it's sqlite that's bringing you trouble. I encountered this one before - it seems sqlite can't understand 'table_name.column_name' syntax, even though it's not mentioned on the website. In my case I just switched to MySQL, and the whole thing went without a problem. See if this helps.
The problem is that you have a model named Object. Give it a name that has no overlap with Ruby system classes, such as ApplicationObject or MediaObject.
I could speculate on why that's causing the issues it is, but it would only be speculation. Open classes is a wonderful feature of Ruby, but it means you need to be careful naming your classes so they're distinct from Ruby's and Rail's classes.