我已经确认这种方法有效。基本上它从控制器获取电子邮件并更改特定用户的电子邮件。
但是,它从未真正保存数据。我传递了错误的电子邮件格式,如果我传递了正确的电子邮件方法返回 true,则它返回 false,这意味着它分配了一个新电子邮件并称为安全。
# 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
我首先检查了日志是否有任何提示,但我得到的只是:
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)
有一种方法来更改此属性也很有意义。由于我使用 Devise gem 进行身份验证,我可以使用current_user
变量来检索当前登录用户的用户对象,然后只需调用current_user.email = newmail; current_user.save
控制器。