我不断吸引用户尝试使用“注册”表单“登录”。它与设计选择有很大关系,但我真的很喜欢包罗万象的想法。
我只是找到了自己的解决方案。我不喜欢它,但它很简单(一旦我终于弄明白了)。我覆盖了stackoverflow 上RegistrationsController
的详细信息。这是我在网站主页上的注册表,原因有几个,所以在那里做似乎很合适。
您需要创建app/controllers/registrations_controller.rb
并将代码放在那里:
# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def new
super
end
def create
# This is where the magic happens. We actually check the validity of the
# username (an email in my case) and password manally.
email = params[:user][:email]
if user = User.find_by_email(email)
if user.valid_password?(params[:user][:password])
sign_in(user)
redirect_to '/'
return
end
end
# Default devise stuff
build_resource(sign_up_params)
resource_saved = resource.save
yield resource if block_given?
if resource_saved
if resource.active_for_authentication?
set_flash_message :notice, :signed_up if is_flashing_format?
sign_up(resource_name, resource)
respond_with resource, location: after_sign_up_path_for(resource)
else
set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
expire_data_after_sign_in!
respond_with resource, location: after_inactive_sign_up_path_for(resource)
end
else
clean_up_passwords resource
@validatable = devise_mapping.validatable?
if @validatable
@minimum_password_length = resource_class.password_length.min
end
respond_with resource
end
end
def update
super
end
end
您需要确保配置路由以使用此控制器,如我在上面链接到的其他 SO 帖子中所述。
# app/config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}
祝你好运!