覆盖Devise的注册控制器,以便在sign_up成功完成后允许重定向

我到处都看了,发现了很多信息……但没有什么对我有用而且我没有得到它:(

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

class Users::RegistrationsController < Devise::RegistrationsController def after_sign_up_path_for(resource) authors_waiting_path end end 

然后按照Tony Amoyal http://www.tonyamoyal.com/2010/07/28/rails-authentication-with-devise-andcancan-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 

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

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

这真令人沮丧。

亚历克斯

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

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

编辑2:

我把控制器的虚拟覆盖:

  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 

我从来没有在我的日志中“在这里”….我真的感觉它完全无视覆盖……我一定是做错了什么:(

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

创建文件夹app / controllers / users

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

 class Users::RegistrationsController < Devise::RegistrationsController def create session["#{resource_name}_return_to"] = complete_path super end end 

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


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

所以你的控制器看起来像

 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 

第二个选择尝试monkeypatch帮助....

 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 

我已经尝试了上面的解决方案,虽然它工作,阅读设计代码,我发现你只需注册注册用户和重定向所需的所有内容是:

  1. 添加is_approved或类似于您的用户表和
  2. 添加active_for_authentication? 用户模型中的方法

码:

 class User < ActiveRecord::Base # ... some code def active_for_authentication? super && is_approved end end 

我需要的时候有点难以找到,但就是这样。 我实际上是在这里写它,以防其他人需要它。