简体   繁体   中英

Rails Devise after_sign_in_path_for(resource) method not functioning as expected

I want to redirect an inactive user to the registration path to collect some information. Here are two approaches I took but neither is working:

I overrode the devise 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 upto debugger, I see that devise does call after_sign_in_path_for. Also, the correct url is being generated by this call:

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

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

I have tried moving the above method to application_helper.rb, session_controller.rb (by extending Devise::SessionController) and session_helper.rb

The issue is that devise does call this method to retrieve the url but it never attempts the redirect. I checked the web server logs, and devise directly goes to the user_root url.

Here is the relevant devise configuration from routes.rb:

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

Are you sure you want to redirect to /users/edit.1 ? Rails will pick that up as if you're trying to access the 1 mime-type instead of html.

The user registration path doesn't need an id, because it always belongs to the currently signed in user. This 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 spot. If you have your own sessions controller, like Users::SessionsController , which inherits from Devise::SessionsController , than it can go in there too.

So either the account_active method doesn't do what you think it does, or you've screwed up the routes file. Try working with a more vanilla configuration in your routes to see if that is the case:

devise_for :users

PS. as a complete an utterly unrelated side note: please try to use Ruby coding conventions, like no semicolons when they're not needed, no parenthesis in if statements, 2-spaces indenting and no unneeded return statements.

This may not apply to you, but in my recent use of devise + active_admin, I ran into the same problems you are describing. I added the devise override while my development rails server was running, and assumed rails/devise would automatically pick up the method. Apparently not since the problem was solved when I . 时问题得到了解决。

It seems that devise is cherry picking these methods off ApplicationController when it initializes, though I havent looked at the source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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