Rails - acts_as_taggable_on Removes Commas When Editing
Asked Answered
E

3

12

I have successfully set up acts_as_taggable_on my model. As expected, when I split the tags with commas, it splits the tags correctly.

However, when I edit the post the field is auto populated with the tags for editing, except the commas are now gone.

This means if I hit save without putting them back in, the tags now become all one tag.

I have tried using ActsAsTaggableOn.delimiter = ' ' which works when they are one word tags. But now I have the issue that if i have a two word tag, when I edit and save the post the two word tags now become one word tags.

Any help anybody might have on this would be greatly appreciated.

Thanks!

Earwig answered 28/2, 2015 at 0:28 Comment(2)
Try this in the form input: <%= f.text_field :tag_list, value: @example_record.tag_list.join(",") %>Wolfsbane
Possible duplicate of Rails 4: text_field for acts_as_taggable_on not separating tags with a commaAttorneyatlaw
L
22

This behavior is apparently by design in acts_as_taggable_on.

Try adding to_s to your tag_list in the form input:

<%= f.text_field :tag_list, value: @example_record.tag_list.to_s %>

Not ideal, but this should allow your field to display the comma separated tags properly.

Leadsman answered 28/2, 2015 at 1:30 Comment(1)
Ah! Worked a treat! Thank you!Earwig
C
4

For simple_form use this:

<%= f.input :tag_list, input_html: {value: @example_record.tag_list.to_s} %>
Childish answered 2/3, 2017 at 11:30 Comment(0)
T
0

If you are using just one word in your tags, you can use a space as a delimiter instead of commas.

config/initializers/acts_as_taggable_on.rb  

ActsAsTaggableOn.delimiter = ' ' # use space as delimiter

I think this is not ideal too, but solved the problem.

Thiamine answered 2/3, 2016 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.