【问题标题】:Rails devise doesn't redirect to root pathRails 设计不会重定向到根路径
【发布时间】:2018-05-16 22:46:05
【问题描述】:

我的user 模型看起来像这样,其中typeuser 表中的列,std 仅用于在用户注册时获取附加数据。现在当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

【问题讨论】:

  • 使用after_sign_up_path_for设计方法(source)
  • sign_up 没有问题 .. sign_in 出现问题
  • devise 还提供了登录方法 (here)。希望这次能有所帮助:)
  • 现在出现这个错误 AbstractController::DoubleRenderError in Devise::SessionsController#create
  • 主要问题是为什么会因为验证而发生这种情况

标签: ruby-on-rails validation devise


【解决方案1】:

已解决.. 我认为它也在登录时验证user(type:Student)所以我只是添加了

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-24
    相关资源
    最近更新 更多