Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 登录后的Spree重定向_Ruby On Rails_Ruby On Rails 4_Spree - Fatal编程技术网

Ruby on rails 登录后的Spree重定向

Ruby on rails 登录后的Spree重定向,ruby-on-rails,ruby-on-rails-4,spree,Ruby On Rails,Ruby On Rails 4,Spree,Spree 2.3和Spree_auth_Desive,Rails 4.0 我正在尝试根据用户的角色在登录后重定向用户。最好的解决方案是在初始值设定项中修改一条路径,一个la Desive,但这些似乎并不存在。下一个最好的解决方案是在会话控制器上创建一个装饰器,但是我找不到这个控制器,当我试图遵循rake routes 在Spree应用程序中,如何根据用户的角色在登录后将其重定向到新位置 更新 在(资源)方法的路径中进行签名后覆盖,会触发该方法,但仍会重新路由到管理路径 # app/contro

Spree 2.3和Spree_auth_Desive,Rails 4.0

我正在尝试根据用户的角色在登录后重定向用户。最好的解决方案是在初始值设定项中修改一条路径,一个la Desive,但这些似乎并不存在。下一个最好的解决方案是在会话控制器上创建一个装饰器,但是我找不到这个控制器,当我试图遵循
rake routes

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

更新 在(资源)方法的路径中进行签名后覆盖
,会触发该方法,但仍会重新路由到
管理路径

# 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
#app/controllers/application_controller.rb
类ApplicationController
我的尝试记录在这里:


提前感谢。

Spree文档似乎涵盖了这一点,请查看:


这似乎是你想要达到的目标的一个很好的起点。

谢谢你在评论中的回答
spree\u auth\u designe
定义了您可以装饰控制器并使用中所述方法的功能:

您可以为以下对象实现自定义方法:

def after_sign_in_path_for(resource)
  current_user_path
end
也许是这样的:

def after_sign_in_path_for(resource)
  if spree_current_user.has_spree_role?("designer")
    root_path
  elsif spree_current_user.has_spree_role?("admin")
    admin_path
  else
    root_path
  end
end
# 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

感谢@mvidaurre花了一些时间和我一起深入研究这个问题

问题是spree_auth_devise有第二个方法尝试重新路由到尝试的最后一个页面,这意味着我不仅需要修改方法的路径中的after_sign_,还需要修改spree方法redirect_back_或默认值(默认值)

此外,因为我的用户通过admin/sign_-in路由登录,所以我需要访问Spree::admin::usersessioncontroller,而不是简单地访问Spree::usersessioncontroller

我的早期解决方案如下所示:

def after_sign_in_path_for(resource)
  if spree_current_user.has_spree_role?("designer")
    root_path
  elsif spree_current_user.has_spree_role?("admin")
    admin_path
  else
    root_path
  end
end
# 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
#app/controllers/application_controller.rb
类ApplicationController
相关的spree源代码如下:

您是否正在使用
spree\u auth\u designe
gem?是的。gem'spree\u auth\u designe',:git=>'',:branch=>'2-3-stable'是的,我都看过了。这对我的问题没有多大帮助。仅供参考,现在是!!!GUIDES.SPREECOMMERCE.ORG!!!最初的报告并不令人鼓舞。用覆盖方法装饰Spree::UserSessionController不起作用,也不是简单地尝试在ApplicationController中覆盖该方法。您能在代码中添加要点吗?看看这个。谢谢你。要点链接刚刚添加到问题中:只是为了确保在尝试中#2可以编写
将spree\u放入当前用户。角色
def after\u登录路径后(资源)
并检查控制台输出当我运行spree\u current\u user.spree\u角色时,返回正确的角色。spree\u当前\u用户。是否具有\u spree\u角色?也如预期的那样工作。