質問

I'm trying to Display each Tag a Question has in a Span. I use acts_as_taggable_on.

I managed to get the below Code working, but it's a Tag cloud. Meaning that ALL the tags are displayed.

<% tag_cloud Question.tag_counts, %w[s m l] do |tag, css_class| %>
    <span class="label label-default">
      <%= link_to tag.name, tag_path(tag.name), class: "css_class" %>
    </span>
  <% end %>

To retrieve the Tags:

question.tag_list

Can someone help me refactor that code so only the CURRENT TAGS ON THE QUESTION are shown ?

役に立ちましたか?

解決

question.tag_list will return you a string, and you can not loop through it.

question.tags will return an array,

<% question.tags.each do |tag| %>
    <span class="label label-default">
      <%= link_to tag.name, tag_path(tag.name), class: "css_class" %>
    </span>
 <% end %>

他のヒント

I am not aware of the plugin, but one problem I see in your code-snippet in general is that you are not operating on a specific object @question, but on the class Question. If I had to take a guess, I would say that this is the source of your problem.

Edit:

So, I just checked out the documentation for the gem and I found this code-snippet directly there:

<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
  <%= link_to tag.name, { :action => :tag, :id => tag.name }, :class => css_class %>
<% end %>

As you can see, this is pretty much what I just told you. Instead of working on the class you have to work on a specific object or, like in the shown case, a collection of objects.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top