简体   繁体   English

注册或新会话后设计自定义重定向

[英]Devise custom redirects after signup or new session

So I have an rss feed with blog posts you can vote on, but first you have to sign up or sign in to vote and I'm using devise to handle this. 因此,我有一个RSS提要,其中包含您可以投票的博客帖子,但是首先您必须注册或登录才能投票,而我正在使用devise来处理此问题。 I went to this page enter link description here 我去了这个页面在这里输入链接描述

to understand how to chagne their default redirect to request.reffer, the current page the user was on before having to sign in or sign up, but it's still redirecting back to the root even after I added this to my application controller: 以了解如何将其默认重定向更改为request.reffer,即用户必须登录或注册之前所在的当前页面,但即使将其添加到应用程序控制器后,它仍会重定向回到根目录:

class ApplicationController < ActionController::Base
  protected
    def after_sign_in_path_for(resource)
      request.env['omniauth.origin'] || stored_location_for(resource) || root_path
    end
end

I'm new to rails and web development so if someone could idiot-proof it to me, that'd be much appreciate. 我是Rails和Web开发的新手,所以如果有人可以对我进行白痴验证,那将不胜感激。 Thanks! 谢谢!

You can go even farther by setting stored_locations_for(resource) to nil, and then have different redirects for each action, ie: after_sign_in_path(resource), after_sign_in_path(resource) and so on. 通过将stored_locations_for(resource)设置为nil,您可以走得更远,然后为每个操作设置不同的重定向,即:after_sign_in_path(resource),after_sign_in_path(resource)等。

routes.rb 的routes.rb

 devise_for :users, controllers: {sessions: 'users/sessions'}

registrations_controller.rb registrations_controller.rb

 class SessionsController < Devise::SessionsController

    protected

      def after_sign_up_path_for(resource)
        example_path
      end
    end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM