Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 设计每个用户类型设置不同的根路径_Ruby On Rails_Ruby On Rails 3_Devise - Fatal编程技术网

Ruby on rails 设计每个用户类型设置不同的根路径

Ruby on rails 设计每个用户类型设置不同的根路径,ruby-on-rails,ruby-on-rails-3,devise,Ruby On Rails,Ruby On Rails 3,Devise,我创建了一个Desive用户模型。有两种类型的用户: 顾客 管理员 我已经完成了bij创建两个“正常”模型:客户和管理员。这两个模型继承自用户模型,如下所示: class Customer < User 更新: 我已经解决了这个问题: root :to => "pages#home", :constraints => lambda { |request|!request.env['warden'].user} root :to => 'customer/dashbo

我创建了一个Desive用户模型。有两种类型的用户:

  • 顾客
  • 管理员
我已经完成了bij创建两个“正常”模型:客户和管理员。这两个模型继承自用户模型,如下所示:

class Customer < User
更新:

我已经解决了这个问题:

root :to => "pages#home", :constraints => lambda { |request|!request.env['warden'].user}
root :to => 'customer/dashboard#index', :constraints => lambda { |request| request.env['warden'].user.type == 'customer' }
root :to => 'admin/dashboard#index', :constraints => lambda { |request| request.env['warden'].user.type == 'admin' }

您可以使用一个根路径,例如
home#index
,并在相应的控制器操作中根据用户类型执行重定向

例如:

def index
  if signed_in?
    if current_user.is_a_customer?
      #redirect to customer root
    elsif current_user.is_a_admin?
      #redirect to admin root
    end
  end
end

虽然这是一个老问题,但没有答案,它可能对其他人有用

在Rails3.2中(我从来没有用更低的版本测试过),您可以在routes.rb文件中进行测试

authenticated :admin_user do
  root :to => "admin_main#index"
end
然后让你的正常根路径再往下走

然而,这在rails 4中似乎不起作用,因为它给出了无效的路由名称,已经在使用:'root'(ArgumentError)(正如我刚刚发现的,当我遇到这个问题时正在寻找解决方案),如果我在rails 4中找到一种方法,我将更新我的答案

编辑:

好的,对于rails 4来说,修复非常简单,但一开始就不那么明显。您只需通过添加as:使第二根路由成为命名路由,如下所示:

authenticated :customer do
  root :to => "customer/dashboard#index"
end

authenticated :admin do
  root :to => "admin/dashboard#index"
end    
authenticated :admin_user do
  root :to => "admin_main#index", as: :admin_root
end
这是有文档记录的,但请注意,这似乎只是一个临时修复,因此可能会在将来再次更改

使用应该是适当的。因此,将此添加到您的
应用程序\u controller.rb

def after_sign_in_path_for(resource)
  if resource.type == 'customer'
    your_desired_customer_path
  else
    your_desired_admin_path
  end
end

谢谢你的回答。您的方法有一个问题:客户根目录和管理员根目录不在domain.com/。它将变成类似于domain.com/customer和domain.com/admin的东西。在这种情况下,您需要将这些条件(是客户吗?是管理员吗?)移动到您的根方法中(您需要为这两种类型的用户使用的常用方法)并根据登录用户获取记录。请将更新后的答案移到问题之外并作为答案。在前8小时内,我无法回答我自己的问题。。。