I have an article, and each article has tags. Right now I call all the tags together:
<% @articles.each do |article| %>
<div class="articlebox">
<article>
<h4>
<%= link_to article.title, article_path(article) %>
</h4>
<%= markdown article.body %>
<span class="articletagbox">
<%= article.tag_tokens %>
</span>
<% end %>
Right now my class goes around all the article tags. I want it to go around each article tag individually.
I’ve tried a simple
<% @tags.each do |tag| %>
but that gives me an undefined method “each” nilclass error.
I know this is pretty simple, but I just can’t figure out what I’m supposed to change to make it work. I assume I need to define something in my article model?
Thanks!
EDIT
So right now my code looks like this:
<% article.tags.each do |tag| %>
<span class="articletagbox">
<%= article.tag_tokens %>
</span>
<% end %>
and the method in article.rb is:
def tag_tokens
self.tags.collect{|t| t.name}.join(", ")
end
which is returning all the tags associated with each article the same number of times as there are tags.
So for example, if I have three tags on an article: tag1, tag2, tag3 I get
<class>tag1 tag2 tag3</class> <class>tag1 tag2 tag3</class> <class>tag1 tag2 tag3</class>
Instead I want
<class>tag1</class> <class>tag2</class> <class>tag3</class>
So I’m just not sure why I’m getting all the tags associated with each article returned together, but the same number of times as there are tags. I hope that makes sense.
If one article
has many :tagsthen in the article loop doFollowing your edit : you’re outputting a method called on the article while looping on tags. Doesn’t make sense, you’re looping on tags, call
tag.nameinstead!Check slhck answer for the specific syntax