Rails simple_form_for creating multiple entries from one input field

164 Views Asked by At

The problem

Screenshot of Stackoverflow tags field

Similar to the tags field on Stackoverflow when you add a question, I would like to add an input field for users to add comma-separated tags when they create new projects.

Simplified DB schema

projects > project_tags (join table) < tags

If a tag does not exist, it would be added to the tags table, then to project_tags to link the project itself.

If the tag already exists, it would simply be linked by adding a connection in project_tags.

Code

# new.html.erb
<%= simple_form_for @project do |f| %>
  <%= f.input :name, label: "Project name" %>
  ...
  <%= f.simple_fields_for :tags do |t| %>
    <%= t.input :tags %>
  <% end %>
  ...
<% end %>
0

There are 0 best solutions below