On 5 January 2012 17:37, Allen Maxwell <aa...@xmission.com> wrote:
> I've worked around this by using a bunch of ugly if statements to update each 
> field individually (not elegant but got the job done when i was first 
> learning)  It is UGLY and not robust so i need to refactor (and improve) it.  
> Here's what i have:

GAH! That certainly is ugly :-)

How does your new approach work now if the user *does* try to change
their password?...


First off, there's no need for all the nesting, just chain them if you
insist on stuff like this:

if @user.update_attribute( :name, params[:user][:name]) &&
 @user.update_attribute( :email, params[:user][:email] ) &&
 @user.update_attribute( :admin, params[:user][:admin] ) &&
 @user.update_attribute( :teamleader, params[:user][:teamleader] ) &&
 @user.update_attribute( :street, params[:user][:street] ) &&
 @user.update_attribute( :city, params[:user][:city] ) &&
 @user.update_attribute( :state, params[:user][:state] ) &&
 @user.update_attribute( :zip, params[:user][:zip] ) &&
 @user.update_attribute( :login, params[:user][:login] ) &&
 @user.update_attribute( :startdate, params[:user][:startdate] ) &&
 @user.update_attribute( :fname, params[:user][:fname] ) &&
 @user.update_attribute( :lname, params[:user][:lname] ) &&
    flash[:success] = "Profile updated."
    redirect_to @user
else
    flash[:failure] = "ERROR: Profile NOT updated."
    @title = "Edit user"
    render :action => "edit"
end

> but i could use some advice on a better way to go :)

You could add some checks to remove the password (and confirmation)
field from the params[:user] hash if there's not been a password
entered. And/or some validation checks in the model that only run the
password validations if a new password and/or confirmation has been
provided. Or you could take your user params hash and assign each
entry to it's attribute and then call "save" (and only update the
password and confirmation if they're not blank in params).... all
sorts of ways.... but probably _not_the way you've chosen ;-)


PS Beware that all those "update_attribute" calls *don't* run validations...

-- 
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 rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to