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

时间:2011-01-24 15:15:57

标签: ruby-on-rails devise override registration

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

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

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-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

是的,我在这里有点奇怪,因为我正在捕捉一些特定的路径将它们发送到注册页面,这使我能够有效地创建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

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

2 个答案:

答案 0 :(得分:9)

好的......我能够覆盖它,所以你应该是: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 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

答案 1 :(得分:3)

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

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

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

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