Ruby on rails 将#u重定向到';我不能正常工作

Ruby on rails 将#u重定向到';我不能正常工作,ruby-on-rails,redirect,devise,Ruby On Rails,Redirect,Devise,我使用design并尝试做下一件事: 当用户登录/注册时,我希望通过他的角色\u id重定向他(对于某些用户,我让id为1,对于其他用户,我让id为2) 如果他的角色id为1,则将其重定向到tasksadmin\u路径,否则重定向到workers\u路径 所以我尝试了以下方法: routes.rb: devise_for :users, :controllers => { :sessions => 'user_sessions'} do get '/users/sign_out

我使用
design
并尝试做下一件事:

当用户登录/注册时,我希望通过他的角色\u id重定向他(对于某些用户,我让id为1,对于其他用户,我让id为2)

如果他的角色id为1,则将其重定向到
tasksadmin\u路径
,否则重定向到
workers\u路径

所以我尝试了以下方法:

routes.rb

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

resources :tasksadmins

resources :workers

root to: "workers#index"
class ApplicationController < ActionController::Base
    include ApplicationHelper

    protect_from_forgery
    before_filter :authenticate_user!

    rescue_from CanCan::AccessDenied do |exception|
        if current_user.role_ids == [2]
           redirect_to root_url
        else
           redirect_to tasksadmins_path
        end
    end
end
这是我的
应用程序\u控制器.rb

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

resources :tasksadmins

resources :workers

root to: "workers#index"
class ApplicationController < ActionController::Base
    include ApplicationHelper

    protect_from_forgery
    before_filter :authenticate_user!

    rescue_from CanCan::AccessDenied do |exception|
        if current_user.role_ids == [2]
           redirect_to root_url
        else
           redirect_to tasksadmins_path
        end
    end
end
class ApplicationController
DeVICE针对这种情况有特殊的方法。您可以在对的路径进行签名后覆盖。应用程序控制器

def after_sign_in_path_for(resource_or_scope)
 if resource_or_scope.is_a?(User)
  town_path
 else
  users_path
 end
end

用于的路径中的签名后的
无效,因此我在“创建”中添加了以下行:

开始时,我写道:

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
然后我在“创建”函数的末尾写道:

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
所以我的创建看起来是这样的:

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
class-usersessioncontrollerresource_name)
需要“uri”
需要“net/http”
##从数据库中获取用户id
user_id=session['warden.user.user.key'][1][0];
##按id获取行
user=user.find(用户id)
# ============================
#确保用户存在
# ============================
代码,body=http_请求(Net::http::Put.new(“/api/v1/users/external/#{user_id}”),email:user.email);
如果代码!=200
错误(“无法在Licensario注册用户#{current_user.email}”);
结束
登录(资源名称、资源)
如果当前_user.role_id==[2]
使用资源响应\u,:location=>workers\u路径
其他的
使用资源响应:location=>tasksadmins\u路径
结束
结束
结束

我尝试了您建议的方法,结果是:在该操作中多次调用了Render和/或redirect。请注意,您只能调用render或redirect,每个操作最多只能调用一次。另外请注意,重定向和渲染都不能终止操作的执行,因此,如果您想在重定向后退出操作,则需要执行类似“重定向到(…)并返回”的操作。我不知道为什么重定向太多。。我写道:如果当前的用户角色ID=[2]重定向到根目录url,否则重定向到任务ADMINS\U路径结束你不应该重定向到这个路径。您应该只给Desive url以重定向。设法改变你的方向。方法的路径中的签名后返回url。如果当前用户角色ID==[2]根url else tasksadmins\u路径结束,则为(用户)在路径中签名后定义请参见前面的注释。使用Desive中的标准方法比钩住会话控制器要好得多。