Skip to content

Rails gem that create dynamic form with accepts_nested_attributes_for.

License

Notifications You must be signed in to change notification settings

kmdtmyk/nested_fields

Repository files navigation

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

About

Rails gem that create dynamic form with accepts_nested_attributes_for.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published