4
我想用戶的role_ids
重定向:DoubleRenderError:「渲染和/或重定向在這次行動中被多次」
- 如果它是等於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"=>"[email protected]",
"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
@MikhalNikalyukin,我想你的建議,並重定向我:HTTP://本地主機:3333 /用戶/ sign_in –
應始終重定向到'_url'而不是隻是'_path';您正在重定向的頁面也可能需要您沒有的身份驗證,然後重定向回登錄頁面。 – PeppyHeppy
@PeppyHeppy,謝謝你,但我有要求認證。我試圖在函數'after_sign_in_path_for'的第一行中打印'current_user.role_ids',並打印'2'。此外,只是應用程序控制器需要身份驗證(before_filter:authenticate_user!) –