1.8k sao GitHub và vẫn tăng — ryanb/nested_form là dự án Ruby mà TopGit đang theo dõi trên nền tảng. Rails plugin to conveniently handle multiple models in a single form.
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
Imagine 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 => true option to that declaration. See the accepts_nested_attributes_for documentation for details on all available options.
This will create a tasks_attributes= method, so you may need to add it to the attr_accessible array (attr_accessible :tasks_attributes).
Then use the nested_form_for helper method to enable the nesting.
<%= nested_form_for @project do |f| %>
You 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.
<%= f.fields_for :tasks do |task_form| %>
<%= task_form.text_field :name %>
<%= task_form.link_to_remove "Remove this task" %>
<% end %>
<p><%= f.link_to_add "Add a task", :tasks %></p>
In order to choose how to handle, after validation errors, fields that are
marked for destruction, the marked_for_destruction class is added on the div
if the object is marked for destruction.
Strong Parameters
For Rails 4 or people using the "strong_parameters" gem, here is an example:
The :id is to make sure you do not end up with a whole lot of tasks.
The :_destroy must be there so that we can delete tasks.
SimpleForm and Formtastic Support
Use simple_nested_form_for or semantic_nested_form_for for SimpleForm and Formtastic support respectively.
Partials
It 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.
<%= f.fields_for :tasks %>
In this case it will look for a partial called "task_fields" and pass the form builder as an f variable to it.
Specifying a Target for Nested Fields
By default, link_to_add appends fields immediately before the link when
clicked. This is not desirable when using a list or table, for example. In
these situations, the "data-target" attribute can be used to specify where new
fields should be inserted.
Note that the :data option above only works in Rails 3.1+. For Rails 3.0 and
below, the following syntax must be used.
<p><%= f.link_to_add "Add a task", :tasks, "data-target" => "#tasks" %></p>
JavaScript events
Sometimes you want to do some additional work after element was added or removed, but only
after DOM was really modified. In this case simply listening for click events on
'Add new'/'Remove' link won't reliably work, because your code and code that inserts/removes
nested field will run concurrently.
This problem can be solved, because after adding or removing the field a set of custom events
is triggered on this field. Using form example from above, if you click on the "Add a task" link,
nested:fieldAdded and nested:fieldAdded:tasks will be triggered, while
nested:fieldRemoved and nested:fieldRemoved:tasks will be triggered if you click
"Remove this task" then.
These events bubble up the DOM tree, going through form element, until they reach the document.
This allows you to listen for the event and trigger some action accordingly. Field element, upon
which action was made, is passed along with the event object. In jQuery you can access it
via event.field, in Prototype the same field will be in event.memo.field.
For example, you have a date input in a nested field and you want to use jQuery datepicker
for it. This is a bit tricky, because you have to activate datepicker after field was inserted.
jQuery
$(document).on('nested:fieldAdded', function(event){
// this field was just inserted into your form
var field = event.field;
// it's a jQuery object already! Now you can find date input
var dateField = field.find('.date');
// and activate datepicker on it
dateField.datepicker();
})
Prototype
document.observe('nested:fieldAdded', function(event){
var field = event.memo.field;
// it's already extended by Prototype
var dateField = field.down('.date');
dateField.datepicker();
})
Second type of event (i.e. nested:fieldAdded:tasks) is useful then you have more than one type
of nested fields on a form (i.e. tasks and milestones) and want to distinguish, which exactly
was added/deleted.
See also how to limit max count of nested fields
Enhanced jQuery JavaScript template
You can override default behavior of inserting new subforms into your form. For example:
If you have any issues with Nested Form not addressed above or in the example project, please add an issue on GitHub or fork the project and send a pull request. To run the specs:
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/ryanb/nested_form là nguồn chính thức.
ryanb/nested_form có phải mã nguồn mở không?
Có — ryanb/nested_form phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/ryanb/nested_form.
ryanb/nested_form có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho ryanb/nested_form. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
ryanb/nested_form có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho ryanb/nested_form. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
ryanb/nested_form còn đang phát triển không?
Commit gần nhất trên ryanb/nested_form là 5.0 năm trước (theo timestamp GitHub). Repo có 501 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
ryanb/nested_form dùng license gì?
ryanb/nested_form phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về nested_form?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về nested_form.