Ruby-on-rails 覆盖 Devise 的注册控制器以允许在成功注册后进行重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4783604/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:11:05  来源:igfitidea点击:

Overriding Devise's registration controller to allow for a redirect after a successful sign_up has been done

ruby-on-railsdeviseoverridingregistration

提问by Alex

I have looked all over the place, and found a lot of info... but nothing works for me and I don't get it :(

我找遍了整个地方,找到了很多信息……但对我来说没有任何用处,我不明白:(

I know that you are suppose to override the registration controller, like this:

我知道您应该覆盖注册控制器,如下所示:

class Users::RegistrationsController < Devise::RegistrationsController

def after_sign_up_path_for(resource)
  authors_waiting_path
end 

end

Then following the example showed by Tony Amoyal http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/, I am supposed to change my routes to update the access the new controller:

然后按照 Tony Amoyal http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-and-cancan-customizing-devise-controllers/展示的例子,我应该改变我的路线更新访问新控制器:

devise_for :users, :controllers => { :registrations => "users/registrations" } do
#get '/author/sign_up', :to => 'devise/registrations#new'
#get '/client/sign_up', :to => 'devise/registrations#new'  
get '/author/sign_up', :to => 'users/registrations#new'
get '/client/sign_up', :to => 'users/registrations#new'      
end

Yes, I have something a bit strange here, because I am catching some specific path to send them to the registration page, this allows me to create effectively 2 registration scenario. I commented what I had before I had overridden the registration controller.

是的,我在这里有点奇怪,因为我正在捕捉一些特定的路径将它们发送到注册页面,这使我能够有效地创建 2 个注册场景。我评论了我覆盖注册控制器之前的内容。

Even with all this and my authors_waiting_path being a valid path, it just keeps on going to the sign-in page after registration :(

即使所有这些以及我的authors_waiting_path 是一个有效路径,它也会在注册后继续进入登录页面:(

This is really frustrating.

这真的很令人沮丧。

Alex

亚历克斯

edit: I also found this on the devise wiki: https://github.com/plataformatec/devise/wiki/How-To:-Redirect-after-registration-(sign-up)

编辑:我也在设计维基上找到了这个:https: //github.com/plataformatec/devise/wiki/How-To: -Redirect-after-registration-(sign- up)

But I have no idea where to define this create method ? should I override the session controller ???

但我不知道在哪里定义这个 create 方法?我应该覆盖会话控制器吗???

edit 2:

编辑2:

I put a dummy override of the controller:

我把控制器的虚拟覆盖:

  class Pouets::RegistrationsController < Devise::RegistrationsController

    def after_sign_up_path_for(resource)
      authors_waiting_path
    end 

    def new
      super
    end

    def create
      puts "was here"
      super
    end

    def edit
      super
    end

    def update
      super
    end

    def destroy
      super
    end

    def cancel
      super
    end

  end

And I never the "was here" in my logs.... I really have the feeling that it's totally ignoring the override... I must be doing something wrong :(

而且我从来没有在我的日志中出现过“在这里”......我真的觉得它完全忽略了覆盖......我一定是做错了什么:(

采纳答案by Piotr M?sior

Ok... I am able to override it so you should be either :0

好的...我可以覆盖它,所以你应该是:0

Create folder app/controllers/users

创建文件夹 app/controllers/users

put there registrations_controller.rb with: (option with session - but it will try sign_in and later redirect - it may be not intended behavior for you ). Furthermore this is from devise wiki and I am not sure if it works

将registrations_controller.rb 放在那里:(带有会话的选项 - 但它会尝试 sign_in 并稍后重定向 - 它可能不是你的预期行为)。此外,这是来自设计维基,我不确定它是否有效

class Users::RegistrationsController < Devise::RegistrationsController

  def create
    session["#{resource_name}_return_to"] = complete_path
    super
  end

end

restart application (just for ensure you don't trust anything)

重新启动应用程序(只是为了确保您不信任任何东西)



All in all you must override Create If you want redirect only Users... if you want define some more complex scenario you should monkeypatch sign_in_and_redirect

总而言之,您必须覆盖 Create 如果您只想重定向用户...如果您想定义一些更复杂的场景,您应该monkeypatch sign_in_and_redirect

so your controller will looks like

所以你的控制器看起来像

class Users::RegistrationsController < Devise::RegistrationsController
  # POST /resource/sign_up
  def create
    build_resource

    if resource.save
      set_flash_message :notice, :signed_up

      #sign_in_and_redirect(resource_name, resource)\
      #this commented line is responsible for sign in and redirection
      #change to something you want..
    else
      clean_up_passwords(resource)
      render_with_scope :new
    end
  end
end

second option try to monkeypatch helper ....

第二个选项尝试使用monkeypatch helper ....

module Devise
  module Controllers
    # Those helpers are convenience methods added to ApplicationController.
    module Helpers
      def sign_in_and_redirect(resource_or_scope, resource=nil, skip=false)
        #intended behaviour for signups
      end
    end
  end
end

回答by Krule

I have tried the above solution and while it works, reading devise code, I have found that all you actually need in order to sign-out just registered user and redirect is:

我已经尝试了上述解决方案,虽然它有效,但阅读设计代码,我发现为了注销刚刚注册的用户和重定向,您实际需要的只是:

  1. to add is_approved or similar to your user table and
  2. to add active_for_authentication? method in your User model
  1. 添加 is_approved 或类似到您的用户表和
  2. 添加active_for_authentication?用户模型中的方法

Code:

代码:

class User < ActiveRecord::Base

  # ... some code

  def active_for_authentication?
    super && is_approved
  end
end

Was a bit hard to find when I needed it, but that is all. I am actually writing it here in case someone else needs it.

我需要的时候有点难找,但仅此而已。我实际上是在这里写的,以防其他人需要它。