Rails设计不会重定向到根路径

问题描述 投票:0回答:1

我的user模型看起来像这样typeuser表中的列,而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

设计:: 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
ruby-on-rails validation devise
1个回答
0
投票

解决..我认为它也验证user(type:Student)at登录时间,所以我刚刚添加

validates_presence_of :std , allow_blank: false , if: -> { type == 'Student' and new_record? },
                         message: 'Student must add grade to continue'

现在它仅在注册时进行验证。

© www.soinside.com 2019 - 2024. All rights reserved.