Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value empty in password text box rails 3?

<%=password_field 'email_setting','emailpasswd' %>

In this @email_setting.emailpasswd has the value password(@email_setting.emailpasswd="password").

But in email_setting form edit the password text box is loaded with empty.Instead of that I need to show some characters(#######) to indicate that the password is already entered.

I'm using Rails 3.

like image 407
Dhinesh Avatar asked Nov 23 '25 13:11

Dhinesh


2 Answers

password_field renders with nil value by default. This makes the use of passwords secure by default. If you want to render the value of the password_field, you have to do for instance

f.password_field(:password, :value => @user.password)

https://github.com/rails/rails/commit/1851af84c1c7244dc416be9c93a4700b70e801e3

like image 87
user3085280 Avatar answered Nov 25 '25 11:11

user3085280


You could also do something like this:

In /config/initializers/my_constants.rb, set a constant with the value that you'd like to display to the user in the password input box.

STARS_PASSWORD = '*******'

In the User model, create a method that we will call from the form to determine what to display to the user inside the password input box.

class User < ActiveRecord::Base
    def password_form_value
        password.present? ? STARS_PASSWORD : ''
    end
end

In the form, display the STARS_PASSWORD as the value on the input form, if in fact the user has already set a password.

<%=form.password_field :password, :value => @user.password_form_value %>

In the UsersController, delete the submitted password before updating if the submitted password is STARS_PASSWORD.

params[:user].delete :password if params[:user][:password] == STARS_PASSWORD

NB: If you have a password validator on the User model (for example, the password needs to have letters and numbers), you will also need to modify that validator to allow STARS_PASSWORD.

like image 29
Naphtali Visser Avatar answered Nov 25 '25 11:11

Naphtali Visser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!