求助:Devise after_sign_in_path_for失效,模型含on: :update验证时被忽略
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 endThen update your
routes.rbto 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 asprotectedorprivate(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:
Adddata: { turbolinks: false }to your login form tag, or explicitly set the form'sformat: :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

