You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Devise注册功能失效,是否与自定义Users Controller冲突?

问题:Devise注册功能突然失效排查

原本正常运行的Devise注册功能突然失效,推测是创建用于编辑用户展示页(包含照片、昵称等信息)的Users Controller后出现的问题,怀疑存在冲突可能性,此前用相同架构搭建应用未遇此类问题。

路由配置

Rails.application.routes.draw do
  devise_for :users
  resources :users
  resources :trainings

  root to: 'pages#home'

  get 'home', to: "pages#home"
  get 'dashboard', to: 'pages#dashboard'
  get 'tutorials', to: "pages#tutorials"
  get 'classads', to: 'pages#classads'

  get 'profile', to: 'users#show'

  get 'sessions', to: 'trainings#sessions'
  get 'my_trainings', to: 'trainings#my_trainings'
end

Users Controller代码

class UsersController < ApplicationController
  def show
    @user = current_user
  end

  def edit
    @user = current_user
  end

  def update
    @user = current_user

    if @user.update(user_params_update)
      redirect_to user_path(@user)
    else
      render :edit
    end
  end

  private

  def user_params
   params.require(:user).permit(:nickname,:address,:email,:tel,:photo, :old, :description, :password)
  end

  def user_params_update
    params.require(:user).permit(:nickname,:address,:email,:tel,:photo, :old, :description, :password)
  end
end

User模型代码

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  has_one_attached :photo, dependent: :destroy

  OLD = ['< 1', '1', '2', '3', '4', '5', '> 5']
  validates_inclusion_of :old, :in => OLD
end

日志信息

Started POST "/users" for ::1 at 2022-12-08 23:24:42 +0100
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"authenticity_token"=>"[FILTERED]", "user"=>{"email"=>"b@b.B", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  TRANSACTION (1.1ms)  BEGIN
  User Exists? (0.9ms)  SELECT 1 AS one FROM "users" WHERE "users"."email" = $1 LIMIT $2  [["email", "b@b.b"], ["LIMIT", 1]]
  TRANSACTION (0.6ms)  ROLLBACK
  Rendering layout layouts/application.html.erb
  Rendering devise/registrations/new.html.erb within layouts/application
  Rendered devise/shared/_links.html.erb (Duration: 0.2ms | Allocations: 81)
  Rendered devise/registrations/new.html.erb within layouts/application (Duration: 8.7ms | Allocations: 6931)
[Webpacker] Everything's up-to-date. Nothing to do
  Rendered shared/_navbar.html.erb (Duration: 0.3ms | Allocations: 159)
  Rendered shared/_flashes.html.erb (Duration: 0.0ms | Allocations: 21)
  Rendered layout layouts/application.html.erb (Duration: 12.9ms | Allocations: 9066)
Completed 200 OK in 247ms (Views: 13.3ms | ActiveRecord: 2.7ms | Allocations: 15283)

排查建议

  • 核心问题指向User模型的验证失败:日志显示事务执行了ROLLBACK,说明用户数据未通过模型验证。模型中添加了validates_inclusion_of :old, :in => OLD,但注册表单未提交old字段,导致验证不通过。
  • 临时注释模型中的validates_inclusion_of :old代码,测试是否能正常注册,以此确认问题根源。
  • 若确认是old字段验证导致,可采取以下方案:
    • 在注册表单中添加old字段的选择框,让用户注册时选择对应选项
    • 给old字段设置默认值(如在数据库迁移中设置default,或在模型中用before_validation赋值)
    • 修改验证规则,允许old字段为空(添加allow_nil: true或allow_blank: true)
  • 检查路由优先级:虽然当前日志显示请求由Devise的RegistrationsController处理,但可尝试将devise_for :users移到resources :users之后,避免路由冲突(不过当前问题大概率和路由无关)
  • 查看Devise注册页面的错误提示:页面未显示错误可能是因为未渲染Devise的错误信息,可在devise/registrations/new.html.erb中添加<%= devise_error_messages! %>查看具体验证错误

内容的提问来源于stack exchange,提问作者Ophe_Ng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 10:35:24