Rails Devise after_sign_in_path_for (resource) method does not work as expected

I want to redirect an inactive user to the registration path in order to collect some information. Here are two approaches I took, but none of them work:

I redefined the method of the after_sign_in_path method as follows (in application_controller.rb ):

def after_sign_in_path_for(resource) debugger if(account_active) return root_path; else return edit_user_registration_path(resource) end end 

When I hooked the code up to the debugger, I see that devise calls call_sign_in_path_for. In addition, this call is generated by the correct URL:

 (rdb:2) after_sign_in_path_for(resource) "/users/edit.1" 

However, when I look at the server logs, no attempt is made to redirect to "/users/edit.1" under any circumstances.

I tried to move the above application_helper.rb method , session_controller.rb (by extension Devise :: SessionController) and session_helper.rb

The problem is that devise calls this method to retrieve the url but never tries to redirect. I checked the web server logs and the development method jumps directly to user_root url.

Here is the appropriate development configuration from .rb routes:

 devise_for :users do resource :registration, only: [:new, :create, :edit, :update], path: 'users', path_names: { new: 'sign_up' }, controller: 'devise/registrations', as: :user_registration do get :cancel end root :to => "home#index" end match '/user' => "products#index", :as => 'user_root' 

Any suggestions on what I should try?

Thanks,

Tabrez

+4
source share
2 answers

Are you sure you want to redirect to /users/edit.1 ? Rails will select this as if you are trying to access type 1 mime-type instead of html.

The user registration path does not need an identifier, because it always belongs to the currently signed user. That should be enough:

 def after_sign_in_path_for(resource) if account_active root_path else edit_user_registration_path end end 

Also, placing it in the ApplicationController is the right place. If you have your own session controller, for example Users::SessionsController , which inherits from Devise::SessionsController , it can also go there.

Thus, either the account_active method does not do what you think, it either does or you messed up the routes file. Try working with a more vanilla configuration on your routes to make sure it is:

 devise_for :users 

PS. as a completely completely unrelated note: try using Ruby coding conventions, for example, semicolons when they are not needed, there are no brackets in if , indentation of 2-space, and no unnecessary return statements.

+3
source

This may not apply to you, but in my recent use of devise + active_admin, I ran into the same problems that you are describing. I added an application override while my development rails server was working, and suggested that rails / devise would automatically pick this method. Apparently, this is not the case since the problem was resolved when I restarted my server .

It seems that devise is a cherry, picking these methods with the ApplicationController when it is initialized, although I havent looked at the source.

0
source

Source: https://habr.com/ru/post/1392262/


All Articles