class Book < ApplicationRecord
has_many :comments, dependent: :destroy
accepts_nested_attributes_for :comments, allow_destroy: true
end
<%= form.nested_fields :comments do |f| %>
<%= f.text_field :content %>
<%= f.remove_nested_fields_button 'remove' %>
<% end %>
<%= form.add_nested_fields_button :comments, 'add' %>
Gemfile
gem 'nested_fields', git: 'https://github.com/kmdtmyk/nested_fields', ref: '<commit_hash>'
app/assets/config/manifest.js
//= link_directory ../javascripts .js
app/assets/javascripts/application.js
//= require nested_fields
app/views/layouts/application.html.erb
= javascript_include_tag 'application'
Change html tag
<ul>
<%= form.nested_fields :comments, tag: :li do |f| %>
<%= f.text_field :content %>
<%= f.remove_nested_fields_button 'remove' %>
<% end %>
</ul>
Maximum length
class Book < ApplicationRecord
has_many :tags, dependent: :destroy
accepts_nested_attributes_for :tags, allow_destroy: true
validates_length_of :tags, maximum: 5
end
bundle exec rspec
MIT