I’m using the awesome acts-as-taggable-on gem to implement tagging. One problem that I ran into is that when users come back to the edit page, and you use the standard coding for a field, such as this:
<%= f.text_field :tag_list %>
Then the values show, but they’re not comma separated. The big problem with that is that if the users save the form again and don’t add commas, the values will change.
For example, if the person originally had this in the tag list: “funny,exciting,premium”, then in the edit view, the field would read in as “funny exciting premium”, and if they saved, instead of three different tags, they would now have one tag: “funny exciting premium” which isn’t what anyone wants.
I found a great solution on SO by DaniG2k (assuming that the list for your tags is called “tagged list”:
<% @article.tag_types.each do |tag| %>
<div class="form-group">
<strong><%= f.label tag.to_s.titleize %></strong><br />
<% tag_sym = "#{tag.to_s.singularize}_list".to_sym %>
<% tag_list = "#{tag.to_s.singularize}_list" %>
<%= f.text_field tag_sym, value: @article.send(tag_list).to_s, :placeholder => "Comma-separated list of #{tag.to_s}", class: 'form-control' %>
</div>
<% end %>
Don’t forget to change @article to whatever model you’re working with.