ruby-on-rails – Devise / ActionMailer发送重复的电子邮件以进行注册确认

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Devise / ActionMailer发送重复的电子邮件以进行注册确认前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的rails应用程序使用devise来处理注册,身份验证等.我正在使用可确认模块.错误是这样的 – 当用户注册电子邮件时,Devise正在发送两封带有不同确认链接的确认电子邮件.一个链接有效,另一个链接用户指向错误页面.

Devise吐出与错误相关的消息:“确认令牌无效”并将用户带到重新发送确认电子邮件页面.

我正在托管heroku并使用sendgrid发送电子邮件.更新:该bug也发生在localhost上.

我不知道这个bug的根源在哪里,这可能比你需要看到的更多代码

车型/ user.rb

  1. ...
  2.  
  3. devise :database_authenticatable,:registerable,:omniauthable,:recoverable,:rememberable,:trackable,:validatable,:confirmable,:authentication_keys => [:login]
  4.  
  5. ...
  6.  
  7. ## callbacks
  8. after_create :account_created
  9.  
  10. # called after the account is first created
  11. def account_created
  12.  
  13. # check if this activiy has already been created
  14. if !self.activities.where(:kind => "created_account").blank?
  15. puts "WARNING: user ##{self.id} already has a created account activity!"
  16. return
  17. end
  18.  
  19. # update points
  20. self.points += 50
  21. self.save
  22.  
  23. # create activity
  24. act = self.activities.new
  25. act.kind = "created_account"
  26. act.created_at = self.created_at
  27. act.save
  28.  
  29. end
  30.  
  31. ...
  32.  
  33. def confirmation_required?
  34. super && (self.standard_account? || self.email_changed)
  35. end
  36.  
  37. ...

控制器/ registrations_controller.rb

  1. class RegistrationsController < Devise::RegistrationsController
  2. def update
  3. unless @user.last_sign_in_at.nil?
  4.  
  5. puts "--------------double checking whether password confirmation is required--"
  6. ## if the user has not signed in yet,we don't want to do this.
  7.  
  8. @user = User.find(current_user.id)
  9. # uncomment if you want to require password for email change
  10. email_changed = @user.email != params[:user][:email]
  11. password_changed = !params[:user][:password].empty?
  12.  
  13. # uncomment if you want to require password for email change
  14. # successfully_updated = if email_changed or password_changed
  15.  
  16. successfully_updated = if password_changed
  17. params[:user].delete(:current_password) if params[:user][:current_password].blank?
  18. @user.update_with_password(params[:user])
  19. else
  20. params[:user].delete(:current_password)
  21. @user.update_without_password(params[:user])
  22. end
  23.  
  24. if successfully_updated
  25. # Sign in the user bypassing validation in case his password changed
  26. sign_in @user,:bypass => true
  27. if email_changed
  28. flash[:blue] = "Your account has been updated! Check your email to confirm your new address. Until then,your email will remain unchanged."
  29. else
  30. flash[:blue] = "Account info has been updated!"
  31. end
  32. redirect_to edit_user_registration_path
  33. else
  34. render "edit"
  35. end
  36. end
  37. end
  38. end

控制器/ omniauth_callbacks_controller

  1. class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  2.  
  3. skip_before_filter :verify_authenticity_token
  4.  
  5. def facebook
  6. user = User.from_omniauth(request.env["omniauth.auth"])
  7. if user.persisted?
  8. flash.notice = "Signed in!"
  9.  
  10. # if the oauth_token is expired or nil,update it...
  11. if (DateTime.now > (user.oauth_expires_at || 99.years.ago) )
  12. user.update_oauth_token(request.env["omniauth.auth"])
  13. end
  14.  
  15. sign_in_and_redirect user
  16. else
  17. session["devise.user_attributes"] = user.attributes
  18. redirect_to new_user_registration_url
  19. end
  20. end
  21. end

配置/ routes.rb中

  1. ...
  2.  
  3. devise_for :users,controllers: {omniauth_callbacks: "omniauth_callbacks",:registrations => "registrations"}
  4.  
  5. ...

如果需要,我很乐意提供更多信息.我也愿意定制/覆盖设计邮件程序的行为,但我不知道如何去做.

非常感谢!

解决方法

解决了!

我能够覆盖Devise :: Mailer并强制执行堆栈跟踪以找出导致重复电子邮件的确切内容. Devise :: Mailer#confirmation_instructions被调用两次,我发现问题出在my:after_create回调中,如下所示:

在models / user.rb中……

  1. after_create :account_created
  2.  
  3. # called after the account is first created
  4. def account_created
  5.  
  6. ...
  7.  
  8. # update points
  9. self.points += 50
  10. self.save
  11.  
  12. ...
  13.  
  14. end

以某种方式调用self.save会导致邮件再次被触发.我通过改变添加点的时间来解决问题.我摆脱了after_create调用并覆盖了确认!设计中的方法看起来像这样:

  1. def confirm!
  2. super
  3. account_created
  4. end

所以现在用户记录在确认之后才会被修改(添加点).没有更多重复的电子邮件

猜你在找的Ruby相关文章