[英]Rails devise doesn't redirect to root path
我的user
模型如下所示,其中type
是user
表中的列,而std
仅用于在用户sign_up时获取其他数据。现在,当user(type: Student)
登录时,再次设计了带有消息登录的登录页面成功不会呈现root_path
并且在日志中显示回滚事务,但是当我刷新页面时,它将呈现为root_path
。 仅对于type:Student
发生此问题,当我删除validates_presence_of :std
行时,一切运行正常。
现在的问题是为什么会这样或如何做到?
class User < ApplicationRecord
attr_accessor :std
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
validates_presence_of :type
validates_presence_of :std , allow_blank: false , if: -> { type == 'Student' },
message: 'Student must add grade to continue'
end
Devise :: RegistrationsController.rb
class RegistrationsController < Devise::RegistrationsController
def create
super
if params[:user][:type] == 'Student' and user_signed_in?
current_user.grade = Grade.new({cls: params[:user][:std].to_i})
current_user.save
end
end
private
def sign_up_params
params.require(:user).permit(:email, :password, :password_confirmation, :type, :std)
end
end
解决了..我认为它也在登录时验证user(type:Student)
,所以我刚刚添加了
validates_presence_of :std , allow_blank: false , if: -> { type == 'Student' and new_record? },
message: 'Student must add grade to continue'
现在,仅在注册时才进行验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.