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.

FormHelper problem with write-only fields

#5471

With 3.0 RC2 I'm having problems with models that have write-only fields that are not backed by a database column, e.g. a User model with a password field.

I will provide a new test in form_helper_test.rb that fails with RC2 but succeeds with RC.

This is probably due to the changes in 8141f0894ecf869ee11f077e5b49e06669790632 (see ticket #3374.

The only workaround I've found is implementing the xyz_before_type_cast method. Is this the way it's supposed to work?

Reported by Oliver Eilhard · August 27th, 2010 @ 09:24 AM

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

Activity

  1. Oliver Eilhard
    Oliver Eilhard
    • Tag set to form_helper, patch

    This patch adds a failing test to form_helper_test.rb.

    August 27th, 2010 @ 09:29 AM

  2. Santiago Pastorino
    Santiago Pastorino
    • Importance changed from to Low

    I'm about to push a change related with this but definitely is not
    going to solve your problem. You need a database field or a field
    reader. Also your code doesn't work even before my commit. You can
    check it out ...

    Let's review it ...

    object.respond_to?(method_name + "_before_type_cast") ?
    object.send(method_name + "_before_type_cast") :
    object.send(method_name)
    

    Your object doesn't respond_to password_before_type_cast so this code
    ends trying to execute object.send('password') and you don't have a
    reader neither.

    Oliver, perhaps what you want to do is f.password_field :password, :value => ''
    and instead of writing the password= method you can do attr_writer :password ;)

    August 27th, 2010 @ 12:30 PM

  3. Repository
    Repository
    • State changed from new to committed

    (from [f95ba5c262bfd713f0a2fc656f8e645d3eea60f2]) Make InstanceTagMethods#value_before_type_cast raise if the model don't respond to attr_before_type_cast or attr method

    [#3374 text_field() does not use overridden model accessors] [#5471 FormHelper problem with write-only fields state:committed] http://github.com/rails/rails/commit/f95ba5c262bfd713f0a2fc656f8e64...

    August 27th, 2010 @ 01:08 PM

  4. Repository
  5. Oliver Eilhard
    Oliver Eilhard

    I'm still puzzled why it did work up to 3.0 RC, but your help is greatly appreciated. I'm going with the f.password_field :password, :value => '' and that's fine with me. Thanks, Santiago.

    August 27th, 2010 @ 01:51 PM

  6. Santiago Pastorino
    Santiago Pastorino

    Doesn't work for me on Rails 3 RC.
    I'm uploading the example for you to see, i'm getting as expected according to the code :) ...

    undefined method `password' for #<Post id: nil, title: nil, created_at: nil, updated_at: nil>
    
    Extracted source (around line #20):
    
    17:   </div>
    18:   <div class="field">
    19:     <%= f.label :password %><br />
    20:     <%= f.password_field :password %>
    21:   </div>
    22:   <div class="actions">
    23:     <%= f.submit %>
    

    August 27th, 2010 @ 04:28 PM

  7. Oliver Eilhard
    Oliver Eilhard

    Thanks again. Sorry for causing so much work. Next time, I'll do better testing... promised. :-)

    August 27th, 2010 @ 04:41 PM