views:

22

answers:

1

I'm using Ruby 1.8.7, Rails 2.3.8, and using Authlogic 2.1.6 for registration, login, logout. In fact, those three things are nearly all I've done!

During registration, if the user has errors in their registration information, I want the password and password_confirmation fields to be empty when the view is redrawn. I assumed one could clear the fields in the UsersController, as follows:

class UsersController < ApplicationController
 before_filter :require_no_user, :only => [:new, :create]

 def create
   @user = User.new(params[:user])
   if @user.save
     @user.save
     flash[:success] = "Registration successful!"
     redirect_back_or_default root_path
   else
     @user.password = ""
     @user.password_confirmation = ""
     @page_title = "Registration"
   end
 end
 ...

The password_confirmation field is successfully cleared by this code, but the password field is not. I inserted a debugger call just after the 'else', and sure enough, the value of @user.password is unchanged by the line '@user.password'. I am guessing that there is something in Authlogic that is restoring the field's value after I attempt to clear it. (I also tried setting it to 'nil', with the same result).

I've come up with a work-around, I'm explicitly setting the form field's value to "" in the view:

              <%= form.password_field :password, :value => "" %>

But that doesn't seem to me the ideal way to handle it!

+1  A: 

You can do what you have already or:

def create
  if @user_session.save
    ...
  else
    params[:users][:password] = ''

Personally I would stick with what you have.

mark
That one works as well. I think I like your work-around better than mine because feels more correct to me to have that sort of thing in the controller, rather than the view...
Iain
Okay, I lied. your solution doesn't work. I went back to using the value => "" in the view to solve the issue. About the only thing clear to me at this point is that I don't know when/how the field data is being populated. :)
Iain
Either/or really. You're suppressing output in the view so there is really where it belongs. Convention leans toward slim controllers and I think this is unnecessary clutter. Note the misspelt users rather than user is why it isn't working.
mark