views:

146

answers:

3

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.

A: 

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.

neutrino
Thanks for the quick response. I've run into trouble getting the MySQL Gem working in Rails 2.3.2. Will let you know if it fixes the above issue.
Nick
Finally got MySQL working with rails 2.3.2. Unfortunately it did not fix the issue which had more to do with my lack of understanding of the plugin :).
Nick
A: 

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.

Sarah Mei
And rename all your references to object, @objects, etc., too.
Sarah Mei
my apologies, by naming the model object I was giving a general name to the "thing" the being tagged. I've changed "object" to "article" to make this clearer. Thanks for pointing it out.
Nick
Yeah, now that I think about it, if you actually had a model named Object it would have failed much earlier and more mysteriously. :)
Sarah Mei
A: 

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
Nick