Tag Input

Tag field with suggestions, built on Tagify.

Usage

<%= form_with model: @post do |f| %>
  <%= f.tag_input :tags %>
  <%= f.tag_input :stack, whitelist: %w[Rails Hotwire Turbo], max_tags: 5 %>
  <%= f.tag_input :countries, suggestions_url: countries_path, enforce_whitelist: true %>
<% end %>
Props
PropTypeDefaultDescription
whitelistArraynilSuggestions shown in the dropdown when the field is focused and as you type.
enforce_whitelistBooleanfalseAccept only tags from the suggestions (`whitelist` or `suggestions_url`). Without it, any text becomes a tag on Enter or comma.
max_tagsIntegernilMaximum number of tags. Further tags are not added.
suggestions_urlStringnilURL that returns suggestions as JSON, requested with `?q=` as you type. Items are strings or `{ value:, label: }` objects; for an object the tag shows `label` and the form submits `value`.
valueString | Arrayattribute valueInitial tags, comma-separated or as an array. Defaults to the attribute on the form object. The form submits the tags as one comma-separated string, e.g. `Rails,Hotwire`.
placeholderStringnilText shown while the field has no tags.
sizeSymbol
:sm | :md | :lg
:mdSize of the field.
labelString | falsehumanized attribute nameCustom label text. Set to `false` to hide the label.
hintStringnilHelper text displayed below the field.
errorStringnilCustom error message. ActiveRecord errors are displayed automatically.
requiredBooleanautoMarks the field as required. Detected from presence validators on the model.
disabledBooleanfalseShows the tags but does not allow adding or removing them.

Also accepts any HTML attributes via **options (e.g., id:, data:, aria:). class: is also supported for custom styling.

Examples

Basic Tag Input

<%= form_with url: root_path do |f| %>
  <%= f.tag_input :tags, placeholder: "Add tags" %>
<% end %>

With Suggestions

<%= form_with url: root_path do |f| %>
  <%= f.tag_input :stack, whitelist: %w[Rails Hotwire Turbo Stimulus Tailwind React Svelte Python], placeholder: "Add a technology" %>
<% end %>

Only From the List

Pick from the list

<%= form_with url: root_path do |f| %>
  <%= f.tag_input :departments, whitelist: %w[Design Frontend Backend DevOps QA], enforce_whitelist: true, hint: "Pick from the list" %>
<% end %>

Limited Number of Tags

Up to 3 skills

<%= form_with url: root_path do |f| %>
  <%= f.tag_input :skills, value: "Ruby, SQL", max_tags: 3, hint: "Up to 3 skills" %>
<% end %>

Remote Suggestions

<%= form_with url: root_path do |f| %>
  <%= f.tag_input :countries, suggestions_url: countries_path, enforce_whitelist: true, placeholder: "Start typing a country" %>
<% end %>

Tag Input Sizes

<%= form_with url: root_path do |f| %>
  <%= f.tag_input :large_field, size: :lg, value: "Large" %>
  <%= f.tag_input :default_field, value: "Default" %>
  <%= f.tag_input :small_field, size: :sm, value: "Small" %>
<% end %>

Custom Label

<%= form_with url: root_path do |f| %>
  <%= f.tag_input :keywords, label: "SEO keywords" %>
  <%= f.tag_input :no_label, label: false, placeholder: "Field without a label" %>
<% end %>

Disabled State

<%= form_with url: root_path do |f| %>
  <%= f.tag_input :locked_tags, value: "Archived, Read only", disabled: true %>
<% end %>

Validation

Can't be blank

This field has a custom error

<% user = User.new %>
<% user.errors.add(:skills, "can't be blank") %>
<%= form_with model: user, url: root_path do |f| %>
  <%= f.tag_input :skills %>
  <%= f.tag_input :skills, error: "This field has a custom error" %>
<% end %>

Are you absolutely sure?

This cannot be undone.