Skip to content

Latest commit

 

History

History
80 lines (57 loc) · 1.31 KB

README.md

File metadata and controls

80 lines (57 loc) · 1.31 KB

Test

NestedFields

Usage

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' %>

Installation

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'

Example

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

Test

bundle exec rspec

License

MIT