设计登录后如何重定向到不同的页面(基于一些参数)? [英] Devise how to redirect to different page (based on some parameter) after sign in?

查看:160
本文介绍了设计登录后如何重定向到不同的页面(基于一些参数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有两个不同的登录表单,两个控制器将通过Devise :: SessionsController签名,问题是成功登录(或失败)后,我需要重定向到特定于控制器的不同页面。我该怎么做我目前在我的Devise :: SessionsController中有这个,

  class SessionsController< Devise :: SessionsController 
def create
resource = warden.authenticate!(:scope => resource_name,:recall =>#{controller_path} #failure)
return sign_in_and_redirect(resource_name ,资源)
end

def sign_in_and_redirect(resource_or_scope,resource = nil)
scope = Devise :: Mapping.find_scope!(resource_or_scope)
resource || = resource_or_scope
sign_in(scope,resource)除非warden.user(scope)== resource
redirect_to dashboard_path
end

def failure
redirect_to index_path
end
end


解决方案

在应用程序控制器

  before_filter:store_location 
def store_location
除非params [:controller] ==devise / sessions
url = #calculate url这里基于您在
会话中传递的params [:token] :user_return_to] = url
end
end

def stored_location_for(resource_or_scope)
会话[:user_return_to] ||超级
结束

def after_sign_in_path_for(资源)
stored_location_for(资源)|| root_path
end


In my application, I have two different login forms from two controller which will both sign_in through the Devise::SessionsController, the problem is after successful sign in (or failure) I need to redirect to different pages that specific to the controller. How can I do this. I currently have this in my Devise::SessionsController, which

class SessionsController < Devise::SessionsController
    def create
        resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure")
        return sign_in_and_redirect(resource_name, resource)
      end

      def sign_in_and_redirect(resource_or_scope, resource=nil)
        scope = Devise::Mapping.find_scope!(resource_or_scope)
        resource ||= resource_or_scope
        sign_in(scope, resource) unless warden.user(scope) == resource
        redirect_to dashboard_path
      end

      def failure      
        redirect_to index_path
      end
end

解决方案

In application controller

  before_filter :store_location
  def store_location
    unless params[:controller] == "devise/sessions"
      url = #calculate the url here based on a params[:token] which you passed in
      session[:user_return_to] = url
    end
  end

  def stored_location_for(resource_or_scope)
    session[:user_return_to] || super
  end

  def after_sign_in_path_for(resource)
    stored_location_for(resource) || root_path
  end

这篇关于设计登录后如何重定向到不同的页面(基于一些参数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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