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 <a href="http://railstutorial.org/chapters/sign-up#sec:signup_exercises">railstutorial.org.</a> 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] -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

