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

Rails5:买家登录重定向失效,报AbstractController::DoubleRenderError错误

Fixing AbstractController::DoubleRenderError for Buyer Sign-In with Devise

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 its create action—no duplicate responses, no error.
  • stored_location_for(resource) will automatically clear the stored URL after it's used, just like redirect_back does, 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:59:24