Rails5:登录后重定向用户无法正常工作

时间:2018-01-23 15:28:12

标签: ruby-on-rails devise

我有一个devise设置,其中包含3个不同的用户,这些用户也拥有自己的sessions/registrations controllers和自己的models

我还为after sign-in path

创建了设置
 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
      redirect_back(fallback_location: root_path)
   else
      permission_denied
   end
  end

admin and seller after sing-in path工作正常,但当buyer signs-in返回此错误时:

AbstractController::DoubleRenderError in Buyers::SessionsController#create

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

错误发生在买方super

内的sessions_contoller.rb
 def create
   super
 end

我正在尝试将buyer重定向到同一页面! 关于如何解决这个问题的任何想法?

2 个答案:

答案 0 :(得分:0)

我找到了解决方法: 在我的application_contoller.rb

before_action :store_buyer_location!, if: :storable_location?


private
  def storable_location?
    request.get? && is_navigational_format? && !devise_controller? && !request.xhr?
  end

  def store_buyer_location!
    # :buyer is the scope we are authenticating
    store_location_for(:buyer, request.fullpath)
  end

并更改买家的登录后路径:

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
      stored_location_for(resource) || super
   else
      permission_denied
   end
  end

答案 1 :(得分:0)

create控制器上注释掉session方法,因为您不需要覆盖此方法,因为没有任何功能而没有devise默认功能。

我不会'知道如何定义Admin SellerBuyer,如果您为那些创建method,那么您可以重写after_sign_in_path_for方法,如下所示

def after_sign_in_path_for(resource)
    if resource.column_name == 'Admin'
        admin_index_path
    elsif resource.column_name == 'Seller'
        stored_location_for(resource) || seller_account_path(current_seller)
    elsif resource.column_name == 'Buyer'
        redirect_back(fallback_location: root_path)
    else
      permission_denied
    end
end

希望有所帮助