Confirmation fields on forms

I am unsure how to use a confirmation only field in my forms. The
perfect example is when registering a user, you want them to type their
password twice but you only want to have one record for it.

The form fields;
user[password]
user[password_confirmation]

form posted;
user = User.new @params[:user]

Error is thrown because there is no method for password_confirmation.

You just need to add:

attr_accessor :password_confirmation

to your user model.

c.

Chad wrote:

I am unsure how to use a confirmation only field in my forms. The
perfect example is when registering a user, you want them to type their
password twice but you only want to have one record for it.

The form fields;
user[password]
user[password_confirmation]

form posted;
user = User.new @params[:user]

Error is thrown because there is no method for password_confirmation.

Cayce B. wrote:

You just need to add:

attr_accessor :password_confirmation

to your user model.

c.

Thanks! that works well… kind of…

It works on my User model above just as expected. On another model, it
doesn’t work as expected…

The form fields;
bank[bank_account]
bank[routing_number]
bank[bank_account_confirmation]
bank[routing_number_confirmation]

In the model;
attr_accessor :bank_account_confirmation
attr_accessor :routing_number_confirmation
validates_confirmation_of :bank_account
validates_confirmation_of :routing_number

Testing with values that should pass validation, it fails. Here is the
failure debugged;
— &id001 !ruby/object:Bank
attributes:
routing_number: “1”
account_id: 3
bank_account: “2”
bank_account_confirmation: “2”
errors: !ruby/object:ActiveRecord::Errors
base: *id001
errors:
routing_number:
- doesn’t match confirmation
bank_account:
- doesn’t match confirmation
new_record: true
routing_number_confirmation: “1”

Another oddity is that in the Bank model the errors will not show up by
printing error_messages_for :bank

If I fail the User model, error_messages_for :user will print the
expected messages.

Any clues?

Thanks to all.

Chad wrote:

I am unsure how to use a confirmation only field in my forms. The
perfect example is when registering a user, you want them to type their
password twice but you only want to have one record for it.

The form fields;
user[password]
user[password_confirmation]

form posted;
user = User.new @params[:user]

Error is thrown because there is no method for password_confirmation.

If you add a validates_confirmation_of to your model, an acessor for the
confirmation is created for you. No attr_accessor needed.

deee