How to submit multiple NEW items via Rails 3.2 mass-assignment

前端 未结 8 1492
醉梦人生
醉梦人生 2020-12-04 06:33

I have a pretty standard use-case. I have a parent object and a list of child objects. I want to have a tabular form where I can edit all the children at once, as rows in th

8条回答
  •  一生所求
    2020-12-04 07:04

    I've came across this user case in all my last proyects, and I expect this to continue, as julian7 pointed, it is necesary to provide a unique id inside the []. In my opinion this is better done via js. I've been dragging and improving a jquery plugin for dealing with this situations. It works with existing records and for adding new records but expects a certain markup and it degrades gracefully, heres the code and an example:

    https://gist.github.com/3096634

    Caveats for using the plugin:

    1. The fields_for call should be wrapped in a

      with data-association attribute equal to the pluralized name of the model, and a class 'nested_models'.

    2. an object should be built in the view just before calling fields_for.

    3. the object fields perse should be wrapped in a

      with class "new" but only if the record is new (cant remember if I removed this requirement).

    4. A checkbox for the '_destroy' attribute inside a label must exist, the plugin will use the label text to create a destroy link.

    5. A link with class 'add_record' should exist within the fieldset.nested_models but outside the fieldset enclosing the model fields.

    Appart from this nuisances its been working wonders for me.
    After checking the gist this requirements must be clearer. Please let me know if you improve on the code or if you use it :).
    BTW, I was inspired by Ryan Bates first nested models screencast.

提交回复
热议问题