Multiple Update Actions that are Relatively the same

Hi there,

I have 2 actions that are rather identical:

Put /account/you/update_password

def update_password
@user = current :user
respond_to do |format|
if @user.update_attributes
(params[:user])
format.html {redirect_to account_url}
else
format.html {render :action => “edit_password”}
end
end
end

PUT /account/you

Update the user object.

def update
@user = current :user
respond_to do |format|
if @user.update_attributes(params[:user])
format.html {redirect_to account_url}
else
format.html {render :action => “edit”}
end
end
end

The only thing that differs between the 2 is in the “else” part {render :action => “edit” or render :action => “edit_password”} lines. In the past when I have encountered a situation like this I’d use one action, and in the forms I’d stick an extra param indicating what action to render if an error occurred. Doing that seemed kind of clunky in my opinion.

This seems like something common, and/or simple that I’m just overlooking. Is going with the extra param in the form the best route?

Thanks,
Dave

I do not know if having 2 methods is best, but to reduce the
duplication you could always create a utility method that does all the
common part and takes a block for the unique part, then evaluates the
block in the else clause.

Michael

Sounds like a good idea. I was considering something like that. I think that might be the best route.

-Dave