Devise redirecting after errors

2019-05-06 13:38发布

问题:

On the sign up and forgot password views in Devise, if you get an error, it redirects you to the parent page.

So on the sign up page (/users/sign_up), if you get an error, it redirects you to /users and shows the error.

On the forgot password page (/users/password/new), if you get an error, it redirects you to /users/password and shows the error.

How can I change it so it does the same thing as the sign in page, if there's an error, it stays on the same page and shows the error.

I've looked through Devise and can't find where the redirect is.

Here's my routes for Devise:

devise_for :users, :skip => [:sessions]
as :user do
  get 'signin' => 'devise/sessions#new', :as => :new_user_session
  post 'signin' => 'devise/sessions#create', :as => :user_session
  get 'signup' => 'devise/registrations#new', :as => :new_user
  post 'signup' => 'devise/registrations#create', :as => :create_user_session
  delete 'signout' => 'devise/sessions#destroy', :as => :destroy_user_session
  get "/account" => "devise/registrations#edit"
end

回答1:

I think the problem is that you have the post 'signup' named incorrectly. What path does your user signup form POST to?

post 'signup' => 'devise/registrations#create', :as => :create_user_session

Should be:

post 'signup' => 'devise/registrations#create', :as => :user_registration

Here's a look at my routes.rb which solved this issue:

as :user do
  get "/signin" => "devise/sessions#new", :as => :new_user_session
  post "/signin" => "devise/sessions#create", :as => :user_session
  delete "/signout" => "devise/sessions#destroy", :as => :destroy_user_session
  get "/signup" => "devise/registrations#new", :as => :new_user_registration
  post '/signup' => 'devise/registrations#create', :as => :user_registration
end


回答2:

It doesn't redirect you anywhere, those are the URLs that Devise posts to.

If you want to edit these URLs, see the wiki for a good starting point: https://github.com/plataformatec/devise/wiki/How-To:-Change-the-default-sign_in-and-sign_out-routes