8

我想由role_ids用户重定向:

  • 如果等于 2,则重定向到workers_path.
  • 如果等于 1,则重定向到tasksadmins_path.

我定义了接下来的事情:

class ApplicationController < ActionController::Base
  include ApplicationHelper

  protect_from_forgery
  before_filter :authenticate_user!

  def stored_location_for(user)
    nil
  end

  def after_sign_in_path_for(user)
     if current_user.role_ids == [2]
       return redirect_to(workers_path)
     else
       return redirect_to (tasksadmins_path)
     end
  end
end

但是当我登录时,我收到了错误:

AbstractController::DoubleRenderError in UserSessionsController#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".
Rails.root: /home/alon/alon/todolist

Application Trace | Framework Trace | Full Trace
app/controllers/user_sessions_controller.rb:5:in `create'
Request

Parameters:

{"utf8"=>"✓",
 "authenticity_token"=>"jRNZkIXvToEhl9YVxaQoa2hLJiSaHI6JAdfpUNAQMJI=",
 "user"=>{"email"=>"worker216@gmail.com",
 "password"=>"[FILTERED]",
 "remember_me"=>"0"},
 "commit"=>"Sign in"}

这是我的user_session_controller.rb

class UserSessionsController < Devise::SessionsController
  include ApplicationHelper

  def create
      response = super

      require "uri"
      require "net/http"

      ## get the user id from the database
      user_id = session['warden.user.user.key'][1][0];

      ## get the row by id
      user = User.find(user_id)

      # ============================
      # Ensure that a user exists
      # ============================

      code, body = http_request(Net::HTTP::Put.new("/api/v1/users/external/#{user_id}"), email: user.email);
      if code != 200
         Rails.logger.error("Unable to register user #{current_user.email} at Licensario");
      end

      response
   end
end

这是我的routes.rb

TODOLIST::Application.routes.draw do

    devise_for :users, :controllers => { :sessions => 'user_sessions'} do
        get '/users/sign_out' => 'devise/sessions#destroy'
    end

    resources :tasksadmins
    resources :workers
    root to: "workers#index"
end
4

3 回答 3

17

你不应该redirect_toafter_sign_in_path_for. 您应该返回网址:

  def after_sign_in_path_for(user)
     if current_user.role_ids == [2]
       return workers_url
     else
       return tasksadmins_url
     end
  end
于 2013-02-03T08:44:59.397 回答
1

我添加到“创建”下一行:

一开始,我写道:

resource = warden.authenticate!(:scope => resource_name)

然后我在“创建”函数的末尾写道:

sign_in(resource_name, resource)

if current_user.role_ids == [2]
   respond_with resource, :location => workers_path
else
   respond_with resource, :location => tasksadmins_path
end

所以我的创作看起来是这样的:

class UserSessionsController < Devise::SessionsController
    include ApplicationHelper

    def create

        resource = warden.authenticate!(:scope => resource_name)

        require "uri"
        require "net/http"

        ## get the user id from the database
        user_id = session['warden.user.user.key'][1][0];

        ## get the row by id
        user = User.find(user_id)

        # ============================
        # Ensure that a user exists
        # ============================

        code, body = http_request(Net::HTTP::Put.new("/api/v1/users/external/#{user_id}"), email: user.email);
        if code != 200
           Rails.logger.error("Unable to register user #{current_user.email} at Licensario");
        end

        sign_in(resource_name, resource)

        if current_user.role_ids == [2]
           respond_with resource, :location => workers_path
       else
           respond_with resource, :location => tasksadmins_path
       end

    end
end
于 2013-02-03T11:51:20.497 回答
0

我在使用devise gem 进行管理员帐户管理时遇到了这个问题,但我的是在生产服务器上。

AbstractController::DoubleRenderError(在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,每个操作最多调用一次。另请注意,重定向和渲染都不会终止操作的执行,因此如果你想在重定向后退出一个动作,你需要做一些类似“redirect_to(...) and return”的事情。):

问题是我想登录到管理仪表板,而不是在浏览器中输入admin_dashboard URL,我在浏览器中输入了admin_sign_in URL,同时,一个管理员帐户已经登录。所以当我尝试登录时它不起作用,因为我无法同时将 2 个帐户登录到管理仪表板。

这是我解决它的方法:

  1. 在浏览器中输入管理仪表板的 URL,这使我登录到以前的帐户
  2. 从管理仪表板上的帐户注销
  3. 然后使用我想要的帐户登录到管理仪表板。

就这样。

我希望这有帮助

于 2020-01-30T14:42:25.537 回答