RoR Tutorial : clear password field

Learning Rails following the Rails Tutorial book I have the following
question.
In chapter 8 at the Exercises part they talk about clearing the
password field.
I tried several things to do this, but till now didn’t find the
solution.
Anyone has a tip how to do this?
Thanks in advance

Marcel Faas wrote:

Learning Rails following the Rails Tutorial book I have the following
question.
In chapter 8 at the Exercises part they talk about clearing the
password field.
I tried several things to do this, but till now didn’t find the
solution.
Anyone has a tip how to do this?

What are you trying to do? (I haven’t seen the book.) What version of
Rails are you using? What have you tried?

Thanks in advance

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

when you have and html form with a password field and some one submits
the
form but there is an error the server sends the form back with its
original
values, so the password field has **** in it, what you usually should do
is
clear it.

For about the 5th time: PLEASE QUOTE WHEN REPLYING!

radhames brito wrote:

when you have and html form with a password field and some one submits
the
form but there is an error the server sends the form back with its
original
values, so the password field has **** in it, what you usually should do
is
clear it.

So just set that field to nil in the controller, or perhaps in an
after_validation callback. There may be other ways…

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

Marcel Faas wrote in post #944158:

Learning Rails following the Rails Tutorial book I have the following
question.
In chapter 8 at the Exercises part they talk about clearing the
password field.
I tried several things to do this, but till now didn’t find the
solution.
Anyone has a tip how to do this?
Thanks in advance

The question comes from railstutorial.org.

The exercise in question:
Oftentimes signup forms will clear the password field for failed
submissions, as shown in Figure 8.12. Modify the Users controller create
action to replicate this behavior. Hint: Reset @user.password.

The solution: SPOLIERS!
In your user controller you defined ‘create’. If the save is
successful(if @user.save) nothing needs to be changed. If the save is
unsuccessful(the ‘else’ condition in your code) we render new again with
error messages. You must reset @user.password before rendering the
‘new’ page by setting it to nil.

def create
@user = User.new(params[:user])
if @user.save
flash[:success] = “Welcome to the Sample App!”
redirect_to @user
else
@title = “Sign up”
@user.password = nil
@user.password_confirmation = nil
render ‘new’
end
end

Note: this code will not work if its placed after render ‘new’. It also
will not work if you place it in ‘def new’ because you aren’t calling
that method, which is also why you have to set @title = “Sign up” again.

Hope that helps,
Dan Luchi
[email protected]

I put:

@title = “Sign up”
@user.password.clear
@user.password_confirmation.clear
render “new”

Regards.