Rails5:买家登录重定向失效,报AbstractController::DoubleRenderError错误
Let's break down why this error is happening and how to fix it cleanly:
The Root Cause
The after_sign_in_path_for method is meant to return a path string, not directly call redirect methods like redirect_back. When you call redirect_back in the Buyer branch, you're triggering a redirect response immediately. But Devise's super call in your Buyers::SessionsController#create is already going to handle a redirect (using the path you return from after_sign_in_path_for). This results in two separate attempts to send a response, hence the DoubleRenderError.
The Fix
Replace the redirect_back call with a path string that replicates the same "return to original page" behavior. Devise already provides stored_location_for(resource) to fetch the URL the user was trying to access before signing in—this is exactly what redirect_back uses under the hood.
Here's the corrected after_sign_in_path_for method:
def after_sign_in_path_for(resource) case resource when Admin admin_index_path when Seller stored_location_for(resource) || seller_account_path(current_seller) when Buyer # Return the stored path, or fall back to root_path (matches redirect_back's behavior) stored_location_for(resource) || root_path else # Make sure this returns a path too, not just a method call permission_denied_path end end
Why This Works
- By returning a string path instead of calling
redirect_back, you let Devise handle the redirect logic in itscreateaction—no duplicate responses, no error. stored_location_for(resource)will automatically clear the stored URL after it's used, just likeredirect_backdoes, so you don't have to worry about leftover values.
Quick Note on Your Sessions Controller
You don't need to change anything in Buyers::SessionsController#create—the super call is correct. Devise will take the path you return from after_sign_in_path_for and perform the single redirect it needs.
内容的提问来源于stack exchange,提问作者Theopap

