我将用户模型中的邮件程序调用移动到自定义Devise控制器,突然不再收到开发中的电子邮件。 SMTP全部在配置中设置并且工作正常(我知道,因为当邮件程序在用户模型中作为:after_create
时它起作用 - 顺便说一下,它不能在生产中工作)。
所以我运行了tail -f log/development.log
,点击提交以查看我的Devise扩展程序是否存在路由冲突,并在我的#create操作的输出中找到了这个
Started POST "/users" for ::1 at 2016-07-03 14:48:06 -0700
Processing by Devise::RegistrationsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"blahblah", "user"=>{"email"=>"j@q.com", "firstname"=>"i", "lastname"=>"i", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
(0.3ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'j@q.com' LIMIT 1
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."email" = 'j@q.com' LIMIT 1
SQL (0.6ms) INSERT INTO "users" ("email", "encrypted_password", "firstname", "lastname", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["email", "j@q.com"], ["encrypted_password", "$2a$10$8YxM8UHDnwS8lcxhiE6Gn.VAbLo8bE8HggCQVuIPl8XYXaevHt8F."], ["firstname", "i"], ["lastname", "i"], ["created_at", "2016-07-03 21:48:06.899722"], ["updated_at", "2016-07-03 21:48:06.899722"]]
(0.4ms) COMMIT
看起来我的Devise扩展程序甚至没有被使用?
这里是#create动作
class RegistrationsController < Devise::RegistrationsController
def create
@user = User.new(sign_up_params)
if @user.save
UserMailer.new_user(@user).deliver_now
#AdminMailer.new_user_waiting_for_approval(@admin).deliver_now
else
render :action => 'new'
end
end
private
def sign_up_params
params.require(:user).permit(:firstname, :lastname, :email, :password, :password_confirmation, :approved)
end
end
我的用户模型
class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :items, dependent: :destroy
validates :email, presence: true, uniqueness: true, if: -> { self.email.present? }
validates :firstname, presence: true
validates :lastname, presence: true
def active_for_authentication?
super && approved?
end
def inactive_message
if !approved?
:not_approved
else
super # Use whatever other message
end
end
end
然后我的路线是devise_for :users, :controller => { registrations: 'registrations'}
所以我想这是一个两部分的问题:
User Exists
是否表示什么?答案 0 :(得分:2)
首先将您的设备路由配置更改为:
devise_for :users, controllers: { registrations: 'registrations'}
在您的用户验证中,我认为您应该删除if
选项
validates :email, presence: true, uniqueness: true
我认为应该解决它。为什么?因为唯一性首先会检查数据库中是否存在记录,所以if
选项也会这样做。不完全确定我的想法是否正确,但我认为应该是问题。
如果我能够提供帮助,请告诉我。
PS:还有一个已知的Rails错误,它也会向日志发送两次SQL语句,我认为它已被修复或者有一个待处理的PR,这是由于设置了不同的记录器你的Rails应用程序。我不认为你的与此有关,因为你的SQL语句执行的时间差异。