Usage
Requires@yaireo/tagify ^4.34.0
<%= 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
| Prop | Type | Default | Description |
|---|---|---|---|
whitelist | Array | nil | Suggestions shown in the dropdown when the field is focused and as you type. |
enforce_whitelist | Boolean | false | Accept only tags from the suggestions (`whitelist` or `suggestions_url`). Without it, any text becomes a tag on Enter or comma. |
max_tags | Integer | nil | Maximum number of tags. Further tags are not added. |
suggestions_url | String | nil | URL 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`. |
value | String | Array | attribute value | Initial 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`. |
placeholder | String | nil | Text shown while the field has no tags. |
size | Symbol:sm | :md | :lg | :md | Size of the field. |
label | String | false | humanized attribute name | Custom label text. Set to `false` to hide the label. |
hint | String | nil | Helper text displayed below the field. |
error | String | nil | Custom error message. ActiveRecord errors are displayed automatically. |
required | Boolean | auto | Marks the field as required. Detected from presence validators on the model. |
disabled | Boolean | false | Shows 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
<%= 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
<%= 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
<% 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 %>