设计方法after_sign_in_path_for不起作用 [英] devise method after_sign_in_path_for doesnt work

查看:46
本文介绍了设计方法after_sign_in_path_for不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户登录后,我需要控制重定向.我发现此问题的许多答案都指向after_sign_in_path_for方法.所以我将其添加到我的application_controller.rb:

I need to control redirecting after user is logged in. I found many answers to this problem point to after_sign_in_path_for method. So i added this to my application_controller.rb:

def after_sign_in_path_for(resource_or_scope)
    raise "SIGN IN TEST"
    dashboard_path
end

查看其工作原理.但是无论何时我登录,都不会引发任何异常,方法将被忽略.我该怎么做才能使设计发挥作用?

to check out how this works. But whenever i log in, there are no exceptions raised, method is just ignored. What shall I do to make devise use it?

编辑根据要求,提供route.rb的片段

EDIT as requested, fragment of routes.rb

devise_for :users, :controllers => {
    :sessions => "user/sessions",
    :registrations => "user/registrations",
    :passwords => 'user/passwords'
  }

推荐答案

-

我们像这样成功地使用了它:

We use this successfully like this:

#app/controllers/application_controller.rb
Class ApplicationController < ActionController::Base
    def after_sign_in_path_for(resource)
        dashboard_path
    end
end

-

如果未调用您的方法,则首先要删除 RAISE 调用,然后通过重新启动服务器来重试.如果这不起作用,则问题可能出在 Devise 在您的系统上的工作方式

If your method is not being called, you'll want to firstly remove the RAISE call and try again by restarting your server. If this does not work, the issue will likely be with how Devise is working on your system

如果您可以尝试删除 RAISE &让我们知道结果,这将是很大的帮助!

If you could try removing RAISE & letting us know the result, it will be a great help!

更新

甚至更佳的是您正在使用 users/sessions 控制器.这是我们的:

EVEN BETTER that you're using users/sessions controller. Here's ours:

class Users::SessionsController < Devise::SessionsController

    ####################
    #    Redirects     #
    ####################

    protected

    #Login Path (if already logged in)
    def after_sign_in_path_for(resource)
        dashboard_path
    end

    #Logout Path
    def after_sign_out_path_for(resource_or_scope)
        root_url
    end

end

这篇关于设计方法after_sign_in_path_for不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆