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.

sending :id => nil to form helpers does not remove the "id" html attribute

#4559

I have several apps where I don't need the id for anything. Especially now that nested_attributes produce such long ids, I frequently want to omit the "id" html attribute to save bandwidth and rendering time. However, this is not currently possible:

text_field "foo", "bar", :id => nil # => <input type="text" id="foo_bar" name="foo[bar]" />

I plan to submit a patch momentarily.

Reported by Jeff Dean · May 9th, 2010 @ 07:51 AM

State: resolved
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Jeff Dean
  2. Matt Jones
    Matt Jones

    Interesting idea, but the stated goal ("save bandwidth") smells an awful lot like premature optimization. Are you really generating enough deeply-nested fields to save more than a few bytes?

    This also seems likely to cause more complication for anybody who is trying to generate text_field calls programmatically:

      def some_text_field_helper(name, id=nil)
        text_field some_function_of_name(name), 'something', :id => id
      end
    

    The above is going to be trickier to code with this patch than without.

    May 9th, 2010 @ 04:58 PM

  3. Dan Pickett
    Dan Pickett
    • Tag changed from form_helper, patch to bugmash, form_helper, patch

    May 9th, 2010 @ 05:48 PM

  4. Jeff Dean
    Jeff Dean

    For me the biggest issue is inconsistency. If I pass :class => nil to a text_field, no class attribute is emitted. If I pass :id => nil, the default id is emitted. On that basis alone (principle of least surprise), I think current behavior is a bug.

    I've encountered a number of other places where having no ids is beneficial - for example when I have the same form in multiple places on the same page (think the "new task" form within each task list in Basecamp) and I want the HTML to validate. In that case I'm forced to use an index, which I'm only doing for the purpose of getting valid HTML because Rails won't respect :id => nil.

    Matt - examples like the one you gave would be trivially more complex, but I think that's a totally acceptable price to pay for consistency across attributes.

    May 9th, 2010 @ 07:32 PM

  5. Josh Kalderimis
    Josh Kalderimis

    +1

    Based on the inconsistency of :class and :id behavior

    Is it only text_field, text_area and check_box which are affected?

    May 10th, 2010 @ 11:23 AM

  6. Wijnand Wiersma
    Wijnand Wiersma

    +1

    but I think this should affect all input types.
    Just a quick test reveals it doesn't work for radio buttons.

    May 15th, 2010 @ 12:03 PM

  7. Wijnand Wiersma
    Wijnand Wiersma
    • No changes were found…

    May 15th, 2010 @ 12:03 PM

  8. Wijnand Wiersma
    Wijnand Wiersma

    My previous comment (and sorry for the empty one) was incorrect.
    Using f.radio_button it does skip the id, I did a quick test using radio_button_tag so that explains my incorrect observation.

    The patch seems to be pretty complete to me and works fine.

    May 15th, 2010 @ 01:23 PM

  9. Casey Dreier
    Casey Dreier

    +1 Patch applies just fine. Verified.

    I made a tiny addition of adding a test for verifying that the id is not generated for radio_boxes.

    Note that this patch as implemented does not currently allow you to specify :id => nil for select boxes. Is this something that should be added for consistency?

    May 15th, 2010 @ 08:16 PM

  10. Jeff Dean
    Jeff Dean

    I think it should be consistent. I'm adding support for that now, and will re-post a patch.

    May 15th, 2010 @ 09:15 PM

  11. Rizwan Reza
    Rizwan Reza
    • Tag changed from bugmash, form_helper, patch to bugmash-review, form_helper, patch

    May 15th, 2010 @ 09:15 PM

  12. Jeff Dean
    Jeff Dean

    Thanks Casey. "select" actually did work as expected, and the syntax is:

    select("post", "secret", [], {}, "id" => nil)
    

    I've reattached the patch with an extra test case to support this, but I lost the "Sign-Off" by Casey because I didn't know how to keep it.

    May 15th, 2010 @ 09:26 PM

  13. Repository
    Repository
    • State changed from new to resolved

    May 15th, 2010 @ 09:46 PM

  14. Jeff Dean
    Jeff Dean

    Awesome - thanks José!

    I just realized that I used the arity-2 form of fetch, instead of the block format. Here is a patch that will make this a bit more performant. It's a straight refactoring, and the behavior doesn't change so I don't see the need to change the tests.

    May 15th, 2010 @ 10:13 PM

  15. Casey Dreier
    Casey Dreier

    Sorry, Jeff, my apologies. I was using an incorrect syntax.

    May 15th, 2010 @ 10:45 PM

  16. Rizwan Reza
    Rizwan Reza
    • Tag changed from bugmash-review, form_helper, patch to form_helper, patch

    May 16th, 2010 @ 02:07 AM