DoubleRenderError:“在此操作中多次调用渲染和/或重定向”

时间:2013-02-03 08:07:40

标签: ruby-on-rails redirect devise super internal-server-error

我想通过用户的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

3 个答案:

答案 0 :(得分:15)

您不应该在redirect_to中返回after_sign_in_path_for。你应该返回url:

  def after_sign_in_path_for(user)
     if current_user.role_ids == [2]
       return workers_url
     else
       return tasksadmins_url
     end
  end

答案 1 :(得分:1)

我添加了'创建'下一行:

一开始,我写道:

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

然后我在'create'函数的末尾写道:

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

答案 2 :(得分:0)

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

AbstractController :: DoubleRenderError(此操作多次调用了渲染和/或重定向。请注意,您只能调用渲染或重定向,每个操作最多只能调用一次。另外请注意,重定向和渲染都不会终止执行操作,因此,如果要在重定向后退出操作,则需要执行“ redirect_to(...)并返回”之类的操作。):

问题是我想登录到管理仪表板,而不是在浏览器中输入 admin_dashboard URL,而是在浏览器中输入 admin_sign_in URL,同时,一个管理员帐户已经登录。因此,当我尝试登录时无法正常工作,因为我无法同时登录2个帐户到管理控制台。

这是我的解决方法:

  1. 在浏览器中输入管理控制台的URL,该浏览器使我登录了先前的帐户
  2. 从管理控制台上的帐户注销
  3. 然后使用所需的帐户登录管理控制台。

仅此而已。

我希望这会有所帮助