登录后狂欢重定向

ste*_*eel 2 ruby-on-rails spree ruby-on-rails-4

Spree 2.3 with spree_auth_devise,Rails 4.0

我尝试根据用户的角色登录后重定向用户.最好的解决方案是在初始化程序中修改路径a la Devise,但Spree似乎不存在这些路径.下一个最好的解决方案是在会话控制器上创建一个装饰器,但是我找不到这个控制器,当我尝试跟踪来自的信息时,我也无法访问它rake routes

如何在Spree应用程序中根据用户的角色在登录后将用户重定向到新位置?

更新 覆盖after_sign_in_path_for(resource)方法导致方法被触发,但仍然重新路由到admin_path.

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    byebug # this is triggered
    root_path
  end
end

### OR ####

class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    byebug # this is triggered
    if spree_current_user.has_spree_role?("admin")
      admin_path
    elsif spree_current_user.has_spree_role?("designer")
      new_designers_spree_variant_path
    else
      root_path
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我的尝试记录在这里:https: //gist.github.com/asteel1981/0f258260974f4d748fb5

提前致谢.

ste*_*eel 5

感谢@mvidaurre花了一些时间跟我钻研.

问题是spree_auth_devise有第二种方法尝试重新路由到最后尝试的页面,这意味着我不仅需要修改after_sign_in_path_for方法,还需要修改Spree方法redirect_back_or_default(默认).

另外,因为我的用户通过admin/sign_in路由登录,我需要访问Spree :: Admin :: UserSessionsController,而不仅仅是Spree :: UserSessionsController.

我早期的解决方案如下:

# app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
  def after_sign_in_path_for(resource)
    if spree_current_user.has_spree_role?("admin")
      admin_path
    elsif spree_current_user.has_spree_role?("designer")
      '/designers/spree_variants/new' #rails helper gives the wrong path, not sure why
    else
      root_path
    end
  end
end

# app/controllers/spree/admin/user_sessions_controller.rb
Spree::Admin::UserSessionsController.class_eval do
  def redirect_back_or_default(default)
    if spree_current_user && spree_current_user.has_spree_role?("admin")
      redirect_to(session["spree_user_return_to"] || default)
      session["spree_user_return_to"] = nil
    else
      redirect_to(default)
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

相关的狂欢源代码在这里:https: //github.com/spree/spree_auth_devise/blob/8cb2d325b2c1da02cbe137404d8dda89cc1613a2/lib/controllers/backend/spree/admin/user_sessions_controller.rb