Sterling Duchess
Sterling Duchess

Reputation: 2080

Model/ActiveRecord not saving new data

I have confirmed that this method works. Basically it takes the email from controller and changes the email of the specific user.

However it never actually saves the data. I pass a wrong email format and it returns false if I pass the correct email method returns true which means it assigned a new email and 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 nothing all i get is:

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)

Also does it make much sense to have a method to change this attribute. Since I'm using Devise gem for authentication I can use current_user variable to retrieve the User object for currently logged in user and then just call current_user.email = newmail; current_user.save in the controller.

Upvotes: 5

Views: 1168

Answers (2)

Craig Heneveld
Craig Heneveld

Reputation: 389

Seems that a better way would be to add your custom validations to the email field in your User model, setup your form to submit user params (with new email, here is rails docs to do that 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 you submit action.

Upvotes: 0

fotanus
fotanus

Reputation: 20116

self.save! will throw an exception when not saved.

Also, this might not be right:

self.save
return true

self.save returns true or false according if it successfully saved or not. So you might want to get rid of return true and let the return value be the one returned from self.save

selfkeyword is not needed in this context, neither the 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

That is equivalent to

# 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.

Upvotes: 1

Related Questions