开发者

ActsAsTaggableOn Tagging with checkboxes

开发者 https://www.devze.com 2023-03-31 02:14 出处:网络
I\'m playing with ActsAsTaggableOn in a small project to see what are the possibilities of this gem. So I have Products and products can have tags.

I'm playing with ActsAsTaggableOn in a small project to see what are the possibilities of this gem. So I have Products and products can have tags.

In my Product _form

<%= form_for(@product) do |f| %>
<div class="field">
  <%= f.label :title %><br />
  <%= f.text_field :title %>
</div>
Tags<br />
<% tag_cloud(@tags, %w(css1 css2 css3 css4)) do |tag, css_class| %>
    <%= check_box_tag 'product[tag_list][]',tag.name,@product.tag_list.include? (tag),:class => css_class %>
    <%= tag.name %>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

In ProductController I have defined the tag_cloud method

 def tag_cloud
   @tags = Product.tag_counts_on(:tags)
 end
开发者_如何学运维

So in my editing product, I can tag the product with the checkboxes, but if a product already has tags those checkboxes aren't selected. I assume that here @product.tag_list.include? (tag) I am missing something or doing something wrong.

Any ideas? Thanks for your help =)

Cheers.


Today, after reading more carefully, I found the answer to this one.

This line

<%= check_box_tag 'product[tag_list][]',tag.name,@product.tag_list.include? (tag),:class => css_class %>

should be

<%= check_box_tag 'product[tag_list][]',tag.name,@product.tag_list.include? (tag.name),:class => css_class %>

So instead of the object I want to check if that name exists in the list.

Hope this helps someone.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号