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.

[PATCH] form fields should allow developers to set values to nil

#4839

Currently you cannot set the value of a text_field or hidden_field to nil:

f.text_field :some_field, :value => nil # => will output an input with a value equal to f.object.some_field

I consider this a bug because it is inconsistent with other attributes of text_field. If I specify :class => nil, it will omit the class attribute, but if I specify :value => nil, I get the default value. It is valid HTML to have an input of type "text" with no value attribute, and I think rails should respect the developers intentions if they want to omit it.

Reported by Jeff Dean · June 12th, 2010 @ 01:25 AM

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

Activity

  1. Jeff Dean
    Jeff Dean

    The attached patch contains:

    • a fix for the nil-id
    • tests
    • a minor refactoring to the fetch syntax for better performance (using the block syntax instead of the arity-2 syntax)

    (If that minor refactoring needs it's own ticket and acceptance cycle I can split them out)

    June 12th, 2010 @ 01:29 AM

  2. Paul Barry
    Paul Barry

    +1

    Patch applies cleanly, looks good. Make sense to me too, I would expect providing an explicit value would override the value of the object.

    Out of curiosity, why did you choose to use options.fetch(:foo){ :bar } instead of options.fetch(:foo, :bar)? To avoid the unnecessary method call if the key is present?

    June 12th, 2010 @ 02:01 AM

  3. Jeff Dean
    Jeff Dean

    Thank you for verifying.

    The block syntax of fetch only evaluates the default value if the key is not present in the hash.

    In the options.fetch(:foo, bar) case, the bar method is evaluated even if it's not used. In the options.fetch(:foo){bar} form, the block is only called when it's needed. It's a small performance optimization, but if you are rendering a lot of text fields with nil values on a page, it can add up.

    June 12th, 2010 @ 02:18 AM

  4. Joseph Palermo
    Joseph Palermo

    +1 Works for me.

    This has bitten me several times. Especially the id case where I have to have the same form on the page in two different places (such as the same search form on different parts of the page), and I'd like to be able to remove the ids.

    June 14th, 2010 @ 08:40 AM

  5. Repository
    Repository
    • State changed from new to resolved

    (from [52c922fad1e7260a4af87409d04055af85df25f8]) make text_field and hidden_field omit the value attribute if the developer explicitly passes in :value => nil [#4839 [PATCH] form fields should allow developers to set values to nil state:resolved]

    Signed-off-by: Michael Koziarski michael@koziarski.com

    Conflicts:

    actionpack/lib/action_view/helpers/form_helper.rb
    

    http://github.com/rails/rails/commit/52c922fad1e7260a4af87409d04055...

    June 23rd, 2010 @ 05:25 AM

  6. Repository
    Repository

    (from [ac8d3e3acabf3ece495f591a7195d794d6c611e1]) make text_field and hidden_field omit the value attribute if the developer explicitly passes in :value => nil [#4839 [PATCH] form fields should allow developers to set values to nil state:resolved]

    Signed-off-by: Michael Koziarski michael@koziarski.com
    http://github.com/rails/rails/commit/ac8d3e3acabf3ece495f591a7195d7...

    June 23rd, 2010 @ 05:25 AM

  7. Repository
    Repository

    (from [cbf36cf57c8ba89b5595dfb39b19e1c8ea4956a0]) Revert "make text_field and hidden_field omit the value attribute if the developer explicitly passes in :value => nil [#4839 [PATCH] form fields should allow developers to set values to nil state:reopened]"

    This reverts commit 52c922fad1e7260a4af87409d04055af85df25f8
    http://github.com/rails/rails/commit/cbf36cf57c8ba89b5595dfb39b19e1...

    June 23rd, 2010 @ 06:00 AM

  8. Jeff Dean
    Jeff Dean

    Sorry - that patch didn't come through too well on 2-3-stable. Here's an updated version for 2-3-stable. It applies cleanly for me, and tests pass.

    June 23rd, 2010 @ 07:19 AM

  9. Michael Koziarski
    Michael Koziarski
    • Importance changed from to Low

    As discussed with Jeff over email, there are scenarios where this behaviour change could catch people out, so I'm hesitant to include it in a 2.3 release given how rocky the last one was.

    July 5th, 2010 @ 12:33 AM