简体   繁体   English

覆盖 devise 注册 controller 以进行重定向

[英]Overriding devise registration controller for redirect

Devise functionality needs to be customized and the the RegistrationsController is created.需要自定义 Devise 功能并创建RegistrationsController However, the default set-up for the create action is但是,创建操作的默认设置是

super do |resource|

end

which in itself is a bit of a black box, as it goes to the superclass.当它进入超类时,它本身就是一个黑盒子。 It obvious is wired up for redirection.很明显是为重定向而连接的。 Thus:因此:

super do |resource|
  [...]
  if @user.save?
    redirect_to some_user_attribute_path
  else
    redirect_to a_parameter_based_path
  end
end

is not possible as it will naturally create a是不可能的,因为它自然会创建一个

AbstractController::DoubleRenderError in Users::RegistrationsController#create

Devise wikis only deal with successful actions or all-encompassing approaches. Devise wiki 仅处理成功的操作或包罗万象的方法。

It is a goal to avoid ApplicationController methods, as this use-case has very specific behaviours for only user creation according success or failure (in practice the return is to the same page, but in the case of the failure, is defined via a params[:company][:id] value in lieu of @user.company_id避免 ApplicationController 方法是一个目标,因为这个用例具有非常具体的行为,仅根据成功或失败创建用户(实际上返回到同一页面,但在失败的情况下,通过params[:company][:id]定义params[:company][:id]值代替@user.company_id

How can this be achieved?如何做到这一点?

I think you should override the method completely since you don't want the redirection handling that is after the yield .我认为您应该完全覆盖该方法,因为您不希望在yield之后进行重定向处理 So, you could do this:所以,你可以这样做:

class RegistrationsController < Devise::RegistrationsController

  def create
    build_resource(sign_up_params)

    if resource.save?
      redirect_to some_user_attribute_path
    else
      redirect_to a_parameter_based_path
    end
  end
end

Note: If you need to sign up the user or any other stuff that devise does, you should copy it from the original method注意:如果您需要注册用户或devise所做的任何其他内容,您应该从原始方法中复制它

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

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