Model / ActiveRecord does not save new data

I have confirmed that this method works. It basically takes email from the controller and modifies the email of a particular user.

However, it never saves data. I pass the wrong email format and returns false, if I pass the correct email method, returns true, which means that it assigned a new email and is called safe.

# Allows user to change email address def change_email(newmail) address = EmailVeracity::Address.new(newmail) if address.valid? self.email = newmail self.save return true else return false end end 

I checked the logs first for any hints, but I get nothing:

 Started POST "/members/editmail" for 127.0.0.1 at 2013-04-25 17:33:44 +0200 Processing by MembersController#editmail as HTML Parameters: {"authenticity_token"=>"*****=", "mail"=>"*****@gmail.com"} ←[1m←[35mUser Load (1.0ms)←[0m SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 ←[1m←[36mCharacter Load (0.0ms)←[0m ←[1mSELECT `characters`.* FROM `characters` WHERE `characters`.`user_id` = 1←[0m ←[1m←[35m (0.0ms)←[0m BEGIN ←[1m←[36mUser Exists (0.0ms)←[0m ←[1mSELECT 1 FROM `users` WHERE (`users`.`email` = BINARY '*****@gmail.com' AND `users`.`id` != 1) LIMIT 1←[0m ←[1m←[35mUser Exists (0.0ms)←[0m SELECT 1 FROM `users` WHERE (`users`.`username` = BINARY '******' AND `users`.`id` != 1) LIMIT 1 ←[1m←[36m (0.0ms)←[0m ←[1mROLLBACK←[0m Redirected to http://localhost:3000/members/1 Completed 302 Found in 10ms (ActiveRecord: 1.0ms) 

It also makes sense to use a method to change this attribute. Since I use the Devise gem for authentication, I can use the current_user variable to retrieve the User object for the current user, and then just call current_user.email = newmail; current_user.save current_user.email = newmail; current_user.save in the controller.

+4
source share
2 answers

self.save! throws an exception if it is not persisted.

Also, this may not be true:

 self.save return true 

self.save returns true or false if it is successfully saved or not. That way you can get rid of return true and return the return value back from self.save

In this context, the keyword

self not required, nor are return keywords. So this is equivalent to your code:

 # Allows user to change email address def change_email(newmail) address = EmailVeracity::Address.new(newmail) if address.valid? self.email = newmail save true else false end end 

It is equivalent

 # Allows user to change email address def change_email(newmail) address = EmailVeracity::Address.new(newmail) if address.valid? self.email = newmail save end address.valid? end 

Which also should not be what you want.

+1
source

It seems that the best way would be to add your user checks to the email field in your user model, configure the form for sending user parameters (with a new email address, here are the rails files for this http://guides.rubyonrails.org/form_helpers. html # binding-a-form-to-an-object ) and run something like

 if @user.update_attributes(params[:user], :as => :admin) redirect_to @user, :notice => "User updated." else render :action => 'edit', :alert => "Unable to update user." end 

in your view action.

0
source

All Articles