|
| 1 | +# Stimulus Rails Nested Form |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/stimulus-rails-nested-form) |
| 4 | +[](https://www.npmjs.com/package/stimulus-rails-nested-form) |
| 5 | +[](https://github.com/stimulus-components/stimulus-rails-nested-form) |
| 6 | +[](https://github.com/stimulus-components/stimulus-rails-nested-form) |
| 7 | +[](https://github.com/stimulus-components/stimulus-rails-nested-form) |
| 8 | +[](https://stimulus-rails-nested-form.netlify.com) |
| 9 | + |
| 10 | +## Getting started |
| 11 | + |
| 12 | +A Stimulus controller to create new fields on the fly to populate your Rails relationship with `accepts_nested_attributes_for`. |
| 13 | + |
| 14 | +[Nested attributes](https://apidock.com/rails/ActiveRecord/NestedAttributes/ClassMethods) allow you to save attributes on associated records through the parent. |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +$ yarn add stimulus-rails-nested-form |
| 20 | +``` |
| 21 | + |
| 22 | +And use it in your JS file: |
| 23 | +```js |
| 24 | +import { Application } from "stimulus" |
| 25 | +import NestedForm from "stimulus-rails-nested-form" |
| 26 | + |
| 27 | +const application = Application.start() |
| 28 | +application.register("nested-form", NestedForm) |
| 29 | +``` |
| 30 | + |
| 31 | +## Usage |
| 32 | + |
| 33 | +In your models: |
| 34 | +```ruby |
| 35 | +class User < ApplicationRecord |
| 36 | + has_many :todos |
| 37 | + accepts_nested_attributes_for :todos, reject_if: :all_blank, allow_destroy: true |
| 38 | +end |
| 39 | + |
| 40 | +class Todo < ApplicationRecord |
| 41 | + belongs_to :user |
| 42 | +end |
| 43 | +``` |
| 44 | + |
| 45 | +In your controller: |
| 46 | +```ruby |
| 47 | +class UsersController < ApplicationController |
| 48 | + def update |
| 49 | + if user.update(user_params) |
| 50 | + redirect_to users_path |
| 51 | + else |
| 52 | + render :edit |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + private |
| 57 | + |
| 58 | + def user_params |
| 59 | + params |
| 60 | + .require(:user) |
| 61 | + .permit( |
| 62 | + todos_attributes: [:id, :_destroy, :description] |
| 63 | + ) |
| 64 | + end |
| 65 | +end |
| 66 | +``` |
| 67 | + |
| 68 | +To DRY up the code, we extract the fields in a partial called `todo_form` to use it in the [template](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template) with a new `Todo` and in the default `fields_for`. |
| 69 | + |
| 70 | +In your view: |
| 71 | +```html |
| 72 | +<%= form_with model: @user, data: { controller: 'nested-form' } do |f| %> |
| 73 | + <template data-target="nested-form.template"> |
| 74 | + <%= f.fields_for :todos, Todo.new, child_index: 'NEW_RECORD' do |todo_fields| %> |
| 75 | + <%= render "todo_form", f: todo_fields %> |
| 76 | + <% end %> |
| 77 | + </template> |
| 78 | + |
| 79 | + <%= f.fields_for :todos do |todo_fields| %> |
| 80 | + <%= render "todo_form", f: todo_fields %> |
| 81 | + <% end %> |
| 82 | + |
| 83 | + <!-- Inserted elements will be injected before that target. --> |
| 84 | + <div data-target="nested-form.target"></div> |
| 85 | + |
| 86 | + <button type="button" data-action="click->nested-form#add"> |
| 87 | + Add todo |
| 88 | + </button> |
| 89 | + |
| 90 | + <%= f.submit 'Save todos' %> |
| 91 | +<% end %> |
| 92 | +``` |
| 93 | + |
| 94 | +In the `todo_form.html.erb` partial: |
| 95 | +```html |
| 96 | +<%= f.label :description %> |
| 97 | +<%= f.text_field :description %> |
| 98 | +``` |
| 99 | + |
| 100 | +As explained in the [documentation](https://apidock.com/rails/ActionView/Helpers/FormHelper/fields_for), we need to |
| 101 | + specify the `child_index` and replace its value in JavaScript because the index needs to be unique for each fields. |
| 102 | + |
| 103 | +## Extending Controller |
| 104 | + |
| 105 | +You can use inheritance to extend the functionality of any Stimulus components. |
| 106 | + |
| 107 | +```js |
| 108 | +import NestedForm from "stimulus-rails-nested-form" |
| 109 | + |
| 110 | +export default class extends NestedForm { |
| 111 | + connect() { |
| 112 | + super.connect() |
| 113 | + console.log("Do what you cant here.") |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +These controllers will automatically have access to targets defined in the parent class. |
| 119 | + |
| 120 | +If you override the connect, disconnect or any other methods from the parent, you'll want to call `super.method()` to make sure the parent functionality is executed. |
| 121 | + |
| 122 | +## Development |
| 123 | + |
| 124 | +### Project setup |
| 125 | +```bash |
| 126 | +$ yarn install |
| 127 | +$ yarn dev |
| 128 | +``` |
| 129 | + |
| 130 | +### Tests |
| 131 | + |
| 132 | +[Jest](https://jestjs.io/) and [Puppeteer](https://github.com/puppeteer/puppeteer) are responsible to test this component: |
| 133 | +```bash |
| 134 | +$ yarn test |
| 135 | +``` |
| 136 | + |
| 137 | +### Linter |
| 138 | +[Prettier](https://prettier.io/) and [ESLint](https://eslint.org/) are responsible to lint and format this component: |
| 139 | +```bash |
| 140 | +$ yarn lint |
| 141 | +$ yarn format |
| 142 | +``` |
| 143 | + |
| 144 | +## Contributing |
| 145 | + |
| 146 | +Do not hesitate to contribute to the project by adapting or adding features ! Bug reports or pull requests are welcome. |
| 147 | + |
| 148 | +## License |
| 149 | + |
| 150 | +This project is released under the [MIT](http://opensource.org/licenses/MIT) license. |
0 commit comments