加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 覆盖Devise的注册控制器,以便在成功登录完成后

发布时间:2020-12-16 21:53:14 所属栏目:百科 来源:网络整理
导读:我已经看了整个地方,发现了很多信息…但没有什么对我有用,我没有得到它:( 我知道你是想覆盖注册控制器,像这样: class Users::RegistrationsController Devise::RegistrationsControllerdef after_sign_up_path_for(resource) authors_waiting_pathend end
我已经看了整个地方,发现了很多信息…但没有什么对我有用,我没有得到它:(

我知道你是想覆盖注册控制器,像这样:

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

我从来没有在我的日志中的“在这里”….我真的有这样的感觉,它完全忽视了超越…我一定是做错了事情:(

解决方法

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

创建文件夹app / controllers / users

把这里注册在这里.注意:(选项与会话 – 但它会尝试sign_in和以后的重定向 – 它可能不是您的行为).此外,这是来自devise wiki,我不知道它是否有效

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读