{"id":13400308,"url":"https://github.com/ryanb/nested_form","last_synced_at":"2025-09-27T06:32:29.952Z","repository":{"id":41329919,"uuid":"468501","full_name":"ryanb/nested_form","owner":"ryanb","description":"Rails plugin to conveniently handle multiple models in a single form.","archived":true,"fork":false,"pushed_at":"2021-12-11T21:47:06.000Z","size":420,"stargazers_count":1793,"open_issues_count":84,"forks_count":506,"subscribers_count":35,"default_branch":"master","last_synced_at":"2024-11-17T14:51:24.109Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ryanb.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.rdoc","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-01-12T06:57:26.000Z","updated_at":"2024-10-29T15:49:12.000Z","dependencies_parsed_at":"2022-07-13T11:00:33.310Z","dependency_job_id":null,"html_url":"https://github.com/ryanb/nested_form","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanb%2Fnested_form","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanb%2Fnested_form/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanb%2Fnested_form/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ryanb%2Fnested_form/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ryanb","download_url":"https://codeload.github.com/ryanb/nested_form/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234402271,"owners_count":18826723,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-30T19:00:50.471Z","updated_at":"2025-09-27T06:32:29.522Z","avatar_url":"https://github.com/ryanb.png","language":"Ruby","readme":"# Unmaintained\n\nThe Nested Form gem is **no longer maintained**. Feel free to fork this project.\n\n# Nested Form\n\n[\u003cimg src=\"https://secure.travis-ci.org/ryanb/nested_form.png?branch=master\" alt=\"Build Status\" /\u003e](http://travis-ci.org/ryanb/nested_form)\n\nThis is a Rails gem for conveniently manage multiple nested models in a single form. It does so in an unobtrusive way through jQuery or Prototype.\n\nThis gem only works with Rails 3. See the [rails2 branch](https://github.com/ryanb/nested_form/tree/rails2) for a plugin to work in Rails 2.\n\nAn example project showing how this works is available in the [complex-nested-forms/nested_form branch](https://github.com/ryanb/complex-form-examples/tree/nested_form).\n\n\n## Setup\n\nAdd it to your Gemfile then run `bundle` to install it.\n\n```ruby\ngem \"nested_form\"\n```\n\nAnd then add it to the Asset Pipeline in the application.js file:\n\n```\n//= require jquery_nested_form\n```\n\n### Non Asset Pipeline Setup\n\nIf you do not use the asset pipeline, run this generator to create the JavaScript file.\n\n```\nrails g nested_form:install\n```\n\nYou can then include the generated JavaScript in your layout.\n\n```erb\n\u003c%= javascript_include_tag :defaults, \"nested_form\" %\u003e\n```\n\n## Usage\n\nImagine you have a `Project` model that `has_many :tasks`. To be able to use this gem, you'll need to add `accepts_nested_attributes_for :tasks` to your Project model. If you wish to allow the nested objects to be destroyed, then add the `:allow_destroy =\u003e true` option to that declaration. See the [accepts_nested_attributes_for documentation](http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html#method-i-accepts_nested_attributes_for) for details on all available options.\n\nThis will create a `tasks_attributes=` method, so you may need to add it to the `attr_accessible` array (`attr_accessible :tasks_attributes`).\n\nThen use the `nested_form_for` helper method to enable the nesting.\n\n```erb\n\u003c%= nested_form_for @project do |f| %\u003e\n```\n\nYou will then be able to use `link_to_add` and `link_to_remove` helper methods on the form builder in combination with fields_for to dynamically add/remove nested records.\n\n```erb\n\u003c%= f.fields_for :tasks do |task_form| %\u003e\n  \u003c%= task_form.text_field :name %\u003e\n  \u003c%= task_form.link_to_remove \"Remove this task\" %\u003e\n\u003c% end %\u003e\n\u003cp\u003e\u003c%= f.link_to_add \"Add a task\", :tasks %\u003e\u003c/p\u003e\n```\n\nIn order to choose how to handle, after validation errors, fields that are\nmarked for destruction, the `marked_for_destruction` class is added on the div\nif the object is marked for destruction.\n\n## Strong Parameters\nFor Rails 4 or people using the \"strong_parameters\" gem, here is an example:\n\n```ruby\nparams.require(:project).permit(:name, tasks_attributes: [:id, :name, :_destroy])\n```\n\nThe `:id` is to make sure you do not end up with a whole lot of tasks.\n\nThe `:_destroy` must be there so that we can delete tasks.\n\n## SimpleForm and Formtastic Support\n\nUse `simple_nested_form_for` or `semantic_nested_form_for` for SimpleForm and Formtastic support respectively.\n\n\n## Partials\n\nIt is often desirable to move the nested fields into a partial to keep things organized. If you don't supply a block to fields_for it will look for a partial and use that.\n\n```erb\n\u003c%= f.fields_for :tasks %\u003e\n```\n\nIn this case it will look for a partial called \"task_fields\" and pass the form builder as an `f` variable to it.\n\n\n## Specifying a Target for Nested Fields\n\nBy default, `link_to_add` appends fields immediately before the link when\nclicked.  This is not desirable when using a list or table, for example.  In\nthese situations, the \"data-target\" attribute can be used to specify where new\nfields should be inserted.\n\n```erb\n\u003ctable id=\"tasks\"\u003e\n  \u003c%= f.fields_for :tasks, :wrapper =\u003e false do |task_form| %\u003e\n    \u003ctr class=\"fields\"\u003e\n      \u003ctd\u003e\u003c%= task_form.text_field :name %\u003e\u003c/td\u003e\n      \u003ctd\u003e\u003c%= task_form.link_to_remove \"Remove this task\" %\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c% end %\u003e\n\u003c/table\u003e\n\u003cp\u003e\u003c%= f.link_to_add \"Add a task\", :tasks, :data =\u003e { :target =\u003e \"#tasks\" } %\u003e\u003c/p\u003e\n```\n\nNote that the `:data` option above only works in Rails 3.1+.  For Rails 3.0 and\nbelow, the following syntax must be used.\n\n```erb\n\u003cp\u003e\u003c%= f.link_to_add \"Add a task\", :tasks, \"data-target\" =\u003e \"#tasks\" %\u003e\u003c/p\u003e\n```\n\n\n## JavaScript events\n\nSometimes you want to do some additional work after element was added or removed, but only\nafter DOM was _really_ modified. In this case simply listening for click events on\n'Add new'/'Remove' link won't reliably work, because your code and code that inserts/removes\nnested field will run concurrently.\n\nThis problem can be solved, because after adding or removing the field a set of custom events\nis triggered on this field. Using form example from above, if you click on the \"Add a task\" link,\n`nested:fieldAdded` and `nested:fieldAdded:tasks` will be triggered, while\n`nested:fieldRemoved` and `nested:fieldRemoved:tasks` will be triggered if you click\n\"Remove this task\" then.\n\nThese events bubble up the DOM tree, going through `form` element, until they reach the `document`.\nThis allows you to listen for the event and trigger some action accordingly. Field element, upon\nwhich action was made, is passed along with the `event` object. In jQuery you can access it\nvia `event.field`, in Prototype the same field will be in `event.memo.field`.\n\nFor example, you have a date input in a nested field and you want to use jQuery datepicker\nfor it. This is a bit tricky, because you have to activate datepicker after field was inserted.\n\n### jQuery\n\n```javascript\n$(document).on('nested:fieldAdded', function(event){\n  // this field was just inserted into your form\n  var field = event.field;\n  // it's a jQuery object already! Now you can find date input\n  var dateField = field.find('.date');\n  // and activate datepicker on it\n  dateField.datepicker();\n})\n```\n\n### Prototype\n\n```javascript\ndocument.observe('nested:fieldAdded', function(event){\n  var field = event.memo.field;\n  // it's already extended by Prototype\n  var dateField = field.down('.date');\n  dateField.datepicker();\n})\n```\n\nSecond type of event (i.e. `nested:fieldAdded:tasks`) is useful then you have more than one type\nof nested fields on a form (i.e. tasks and milestones) and want to distinguish, which exactly\nwas added/deleted.\n\nSee also [how to limit max count of nested fields](https://github.com/ryanb/nested_form/wiki/How-to:-limit-max-count-of-nested-fields)\n\n## Enhanced jQuery JavaScript template\n\nYou can override default behavior of inserting new subforms into your form. For example:\n\n```javascript\nwindow.nestedFormEvents.insertFields = function(content, assoc, link) {\n  return $(link).closest('form').find(assoc + '_fields').append($(content));\n}\n```\n\n## Contributing\n\nIf you have any issues with Nested Form not addressed above or in the [example project](https://github.com/ryanb/complex-form-examples/tree/nested_form), please add an [issue on GitHub](https://github.com/ryanb/nested_form/issues) or [fork the project](https://help.github.com/articles/fork-a-repo) and send a [pull request](https://help.github.com/articles/using-pull-requests). To run the specs:\n\n```\nbundle install\nbundle exec rake spec:install\nbundle exec rake db:migrate\nbundle exec rake spec:all\n```\n\nSee available rake tasks using `bundle exec rake -T`.\n\n## Special Thanks\n\nThis gem was originally based on the solution by Tim Riley in his [complex-form-examples fork](https://github.com/timriley/complex-form-examples/tree/unobtrusive-jquery-deep-fix2).\n\nThank you Andrew Manshin for the Rails 3 transition, [Andrea Singh](https://github.com/madebydna) for converting to a gem and [Peter Giacomo Lombardo](https://github.com/pglombardo) for Prototype support.\n\nAndrea also wrote a great [blog post](http://blog.madebydna.com/all/code/2010/10/07/dynamic-nested-froms-with-the-nested-form-gem.html) on the internal workings of this gem.\n\nThanks [Pavel Forkert](https://github.com/fxposter) for the SimpleForm and Formtastic support.\n","funding_links":[],"categories":["Ruby","View Helper","视图"],"sub_categories":["Omniauth"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanb%2Fnested_form","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fryanb%2Fnested_form","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fryanb%2Fnested_form/lists"}