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

求助:Devise after_sign_in_path_for失效,模型含on: :update验证时被忽略

Troubleshooting Devise's after_sign_in_path_for Not Redirecting

Hey Chloe, let's work through why your after_sign_in_path_for method is running but Devise isn't respecting its return value—this is a super common issue with Devise, so let's break down the most likely fixes:

1. Double-check where you've defined the method

Devise expects this method to live in either your ApplicationController or a custom sessions controller (if you've overridden Devise's default sessions behavior). If you put it in a different controller (like UsersController), it won't get picked up.

  • If using a custom sessions controller:
    First, make sure your controller inherits from Devise's base sessions controller:

    class Users::SessionsController < Devise::SessionsController
      protected
    
      def after_sign_in_path_for(resource)
        # Test with a hardcoded path first to rule out logic issues
        edit_user_path(resource) # Or root_path for simplicity
      end
    end
    

    Then update your routes.rb to point Devise to this custom controller:

    devise_for :users, controllers: { sessions: 'users/sessions' }
    
  • If using ApplicationController:
    Just add the method directly there, making sure it's marked as protected or private (Devise looks for it in those scopes):

    class ApplicationController < ActionController::Base
      protected
    
      def after_sign_in_path_for(resource)
        edit_user_path(resource)
      end
    end
    

2. Check if your login request is an AJAX/XHR request

If your login form is submitted via AJAX (common with Turbolinks or JS-powered forms), Devise will return a JSON response instead of a redirect. Even though you see the "Signed in successfully" notice, the page won't redirect because the browser handles AJAX responses differently.

  • Fix this by ensuring your form submits as a regular HTML request:
    Add data: { turbolinks: false } to your login form tag, or explicitly set the form's format: :html.

3. Rule out conflicting redirect logic

Look for any before_action callbacks in your ApplicationController or custom sessions controller that might be overriding Devise's redirect. For example, a callback that checks for a user setting and redirects could be firing after Devise's sign-in logic.

Also, check your config/initializers/devise.rb file—if you've set config.after_sign_in_path_for globally, make sure it's not pointing back to the login page (though controller-level methods take precedence over this global config).

4. Test with a simplified return value

Temporarily replace your custom logic with a hardcoded path like root_path to see if Devise redirects correctly. If it does, the issue is with your original path logic (e.g., edit_user_path might be invalid, or resource is nil when you call it).

You can even add a debug line to confirm what's happening:

def after_sign_in_path_for(resource)
  Rails.logger.info "Signing in user: #{resource.inspect}, redirecting to: #{edit_user_path(resource)}"
  edit_user_path(resource)
end

Check your logs to confirm the path is valid and the resource exists.

5. Verify the session is actually being created

Even though you see the "Signed in successfully" notice, double-check your logs for any hidden errors (like a failed callback on the User model that might be rolling back the session). Look for lines like Completed 200 OK after sign-in—if you see a different status code, that's a clue something's off.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:24:40