Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Add an option to omit the creation of hidden field with object id when using fields_for

#4551

Some so called "ORMs" (CouchRest for example) allow to create inline associations, in this case associated objects does not have its own IDs. An option to omit the creation of hidden field with associated object id would be useful.

Usage:


  # Disable hidden id field
  form_for(@post, :include_id => false) do |f|
    f.text_field :title
    f.fields_for(:comments) do |cf|
      cf.text_field :title

      # Emit ids again
      cf.fields_for(:author, :include_id => true) do |af|
        af.text_field :name
      end
    end
  end

UPDATE: include_id_reworked.patch

Reported by wildchild · May 7th, 2010 @ 06:45 PM

State: committed
Milestone: 3.x
Assigned to: José Valim José Valim
Importance: Low

Activity

  1. wildchild
    wildchild

    Patch included. Can anyone suggest a better option name? Also fixes #4550

    # Disable hidden id field
    form_for(@post, :skip_model_key => true) do |f|
      f.text_field :title
      f.fields_for(:comments) do |cf|
        cf.text_field :title
    
        # Emit ids again
        cf.fields_for(:author, :skip_model_key => false) do |af|
          af.text_field :name
        end
      end
    end
    

    May 9th, 2010 @ 06:14 PM

  2. Dan Pickett
    Dan Pickett
    • Tag changed from formbuilder, rails3 to bugmash, formbuilder, rails3

    May 9th, 2010 @ 06:16 PM

  3. Daniel Cadenas
    Daniel Cadenas

    I've attached a patch

    We changed the option name to include_id and to invert it's boolean value, made it work with the latest 2-3-stable, added some more tests and added some documentation.

    May 16th, 2010 @ 01:19 AM

  4. wildchild
    wildchild

    Another idea is to check that object respond_to?(:to_key) and don't emit hidden id if result is negative? Thoughts?

    June 2nd, 2010 @ 12:31 AM

  5. wildchild
    wildchild

    @Daniel your patch does not applies to master.

    New alternative patch "Don't emit hidden id unless model respond_to?(:to_key)"

    June 17th, 2010 @ 04:38 AM

  6. wildchild
    wildchild
    • Assigned user set to José Valim

    June 20th, 2010 @ 06:47 AM

  7. José Valim
    José Valim

    to_key is a required method to be compliant with ActiveModel API. So if your model does not define to_key, something is really wrong. :include_key => false or :include_id => false seems to be a good way to go.

    Daniel provided a good patch with documentation, it would be nice if the following patch have these as well.

    June 20th, 2010 @ 11:04 AM

  8. José Valim
    José Valim
    • Importance changed from to Low

    Any news?

    July 21st, 2010 @ 02:32 PM

  9. José Valim
    José Valim
    • State changed from new to stale

    Marking as stale. If anyone has an interest in reopening this, please provide a patch.

    September 1st, 2010 @ 08:41 AM

  10. gix
    gix

    Adds an :emit_hidden_id option to #form_for and #fields_for that indicates whether a hidden id field is automatically appended.

    November 5th, 2010 @ 09:59 PM

  11. wildchild
    wildchild

    My previous patch with option name changed to :include_key. Docs included.

    November 9th, 2010 @ 10:48 AM

  12. Ryan Bigg
    Ryan Bigg
    • State changed from stale to open

    Ticket now has patches.

    November 10th, 2010 @ 04:55 AM

  13. wildchild
    wildchild

    Would there be any chance to get it applied before 3.0.2 release?

    November 12th, 2010 @ 04:24 AM

  14. gix
    gix

    Any news on this? Suggestions/Feedback on the patches/approaches? Reasons to not merge one of them?

    Without such a change it's impossible to correctly use non-AR-esque models (for example proper join models with composite keys) without using hacks and getting warnings left and right.

    And please do not invalidate the ticket again just because one of the small patches does not apply cleanly anymore months after it has been posted.

    February 25th, 2011 @ 06:20 AM

  15. wildchild
    wildchild

    @gitx It cannot be applied on master due to some changes in form_helper. I am going to work on new patch this weekend. I think and hope that we should finish this ticket before 3.1.

    February 25th, 2011 @ 06:32 AM

  16. wildchild
    wildchild
    • Tag changed from bugmash, formbuilder, rails3 to bugmash, formbuilder, patch, rails3

    Done. First patch with an option named :include_key and the second with option named :include_id. Docs and tests included. Please apply, test your apps and leave a response.

    February 25th, 2011 @ 10:14 AM

  17. wildchild
    wildchild

    Fixed documentation and code cleaned up according to IRC discussion.

    February 25th, 2011 @ 03:21 PM

  18. Lenary
    Lenary

    that last patch didn't apply to 3-0-stable or master for me. Could you refactor it all into a single patch please, wildchild

    February 28th, 2011 @ 10:28 PM

  19. wildchild
    wildchild

    @Lenary, you must use just include_id_reworked.patch and it applies smoothly on master.

    March 1st, 2011 @ 03:58 AM

  20. wildchild
    wildchild

    Anyone interested in this ticket probably can found #6487 useful too.

    March 4th, 2011 @ 02:46 PM

  21. Santiago Pastorino
  22. Repository
    Repository
    • State changed from open to committed

    (from [0db915efd1240f493c50b7b9f5d1ea5f1e3eec10]) Add an option to FormBuilder to omit hidden field with id

    [#4551 state:committed]

    Signed-off-by: Santiago Pastorino santiago@wyeworks.com
    https://github.com/rails/rails/commit/0db915efd1240f493c50b7b9f5d1e...

    March 4th, 2011 @ 06:43 PM