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
775 views
in Technique[技术] by (71.8m points)

ruby on rails - Devise: redirect on sign up failure?

I'm trying to redirect users that have failed the sign up form (e.g. they entered a username that is already taken, they left a field blank, etc...)

I have custom failure set up for users that fail the sign in form, code below:

class CustomFailure < Devise::FailureApp 

   def redirect_url 
      root_path 
   end 

   def respond 
      if http_auth? 
      http_auth 
   else 
      redirect 
   end 
 end

However, I've been stuck on how to set this up for sign up failure. Ideally I would just like to redirect them back/to root_path, any ideas? Thank you!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You will probably need to subclass Devise::RegistrationsController and override the create action. Just copy over the create method from here and modify the redirect on failure to save.

# app/controllers/registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController


  def create
    # modify logic to redirect to root url
  end


end 

Change your routes to tell Devise to use your controller:

# config/routes.rb
devise_for :users, :controllers => {:registrations => "registrations"}

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

...