Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
430 views
in Technique[技术] by (71.8m points)

ruby on rails - Overriding devise registration controller for redirect

Devise functionality needs to be customized and the the RegistrationsController is created. 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.

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

How can this be achieved?

question from:https://stackoverflow.com/questions/66061571/overriding-devise-registration-controller-for-redirect

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you should override the method completely since you don't want the redirection handling that is after the 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...