Hi all,
I have a user model and associated controller/views. the model has a password field and the security for logging into my site etc. and all of this works pretty well. When i implemented it i had a need to be able to allow users to edit the user data (demographic stuff) and not edit the password (unless they wanted to). The simple way for the code to update the model is to use:
if @user.update_attributes(params[:user]) # tell me i did good else # tell me I screwed up end
this works great if the user changes the password but if they leave it blank (don't want to change that field and go through the hassle of entering in the current password and confirmation) then things go sideways.
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:
if @user.update_attribute( :name, params[:user][:name])
if @user.update_attribute( :email, params[:user][:email] )
if @user.update_attribute( :admin, params[:user][:admin] )
if @user.update_attribute( :teamleader, params[:user][:teamleader] )
if @user.update_attribute( :street, params[:user][:street] )
if @user.update_attribute( :city, params[:user][:city] )
if @user.update_attribute( :state, params[:user][:state] )
if @user.update_attribute( :zip, params[:user][:zip] )
if @user.update_attribute( :login, params[:user][:login] )
if @user.update_attribute( :startdate, params[:user][:startdate] )
if @user.update_attribute( :fname, params[:user][:fname] )
if @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
end
end
end
end
end
end
end
end
end
end
please don't waste your breathe telling me how messed up this approach is: I know… but i could use some advice on a better way to go
thanks in advance.
Max