Add a tag field to forms in Rails 4.2 and above

Augusto Samamé edited this page Dec 1, 2016 · 4 revisions

In Rails 4.2 and above you must construct the field that accepts the tags the following way:

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

This is because ActionView::Helpers::Tags::TextField no longer calls html_safe on its value, which means that for a post with tag_list ['ruby', 'rails'], the following erb block

<%= f.text_field :tag_list %>

produces

<input type="text" value="ruby rails" name="post[tag_list]" id="post_tag_list">

rather than

<input type="text" value="ruby, rails" name="post[tag_list]" id="post_tag_list">

If using simple_form, correct syntax is:

<%= f.text_field :tag_list, input_html: {value: f.object.tag_list.to_s} %>

For more information see #620.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.