Ruby on rails 使用“多个”;“根到”;有2个设计模型

Ruby on rails 使用“多个”;“根到”;有2个设计模型,ruby-on-rails,devise,Ruby On Rails,Devise,我有两个Desive模型,这很好,因为它们捕获了不同的价值,我觉得将它们分离是最好的解决方案,而不是使用角色 话虽如此,我仍在努力支持不同的观点。这是我的路由文件: devise_for :patients, controllers: { sessions: 'patients/sessions', registrations: 'patients/registrations' } as :patient do authenticated

我有两个Desive模型,这很好,因为它们捕获了不同的价值,我觉得将它们分离是最好的解决方案,而不是使用角色

话虽如此,我仍在努力支持不同的观点。这是我的路由文件:

devise_for :patients, controllers: {
        sessions: 'patients/sessions',
        registrations: 'patients/registrations'
      }

  as :patient do
    authenticated  do
      root to: 'dashboard#patient'
    end

    unauthenticated do
      root to: 'shared#home', as: 'unauthenticated_root'
    end
  end

  devise_for :pharmacists, controllers: {
        sessions: 'pharmacists/sessions',
        registrations: 'pharmacists/registrations'
      }

  as :pharmacist do
    authenticated  do
      root to: 'dashboard#pharmacist'
    end

    unauthenticated do
      root to: 'shared#home', as: 'unauthenticated_root'
    end
  end
这是错误消息:

Invalid route name, already in use: 'root' 
You may have defined two routes with the same name using the `:as` option, 
or you may be overriding a route already defined by a resource with the 
same naming.

缓解此问题的最佳方法是什么?

问题源于我没有在实际根上使用“as:”,因此它们最终具有相同的端点。解决此问题后,正确的代码如下所示:

devise_for :patients, controllers: {
        sessions: 'patients/sessions',
        registrations: 'patients/registrations'
      }

  devise_scope :patient do
    authenticated  do
      root to: 'dashboard#patient', as: 'authenticated_patient_root'
    end

    unauthenticated do
      root to: 'shared#home', as: 'unauthenticated_patient_root'
    end
  end

  devise_for :pharmacists, controllers: {
        sessions: 'pharmacists/sessions',
        registrations: 'pharamacists/registrations'
      }

  devise_scope :pharmacist do
    authenticated  do
      root to: 'dashboard#pharmacist', as: 'authenticated_pharmacist_root'
    end

    unauthenticated do
      root to: 'shared#home', as: 'unauthenticated_pharmacist_root'
    end
  end

问题源于我没有在实际根上使用“as:”,因此它们最终具有相同的端点。解决此问题后,正确的代码如下所示:

devise_for :patients, controllers: {
        sessions: 'patients/sessions',
        registrations: 'patients/registrations'
      }

  devise_scope :patient do
    authenticated  do
      root to: 'dashboard#patient', as: 'authenticated_patient_root'
    end

    unauthenticated do
      root to: 'shared#home', as: 'unauthenticated_patient_root'
    end
  end

  devise_for :pharmacists, controllers: {
        sessions: 'pharmacists/sessions',
        registrations: 'pharamacists/registrations'
      }

  devise_scope :pharmacist do
    authenticated  do
      root to: 'dashboard#pharmacist', as: 'authenticated_pharmacist_root'
    end

    unauthenticated do
      root to: 'shared#home', as: 'unauthenticated_pharmacist_root'
    end
  end

您可以使用下面的hacks实现相同的功能,而无需使用
design\u scope

authenticated :patient do
  root to: "patients#show", as: :authenticated_patient
end

authenticated :pharmacist  do
  root "pharmacists#index", as: :authenticated_pharmacist
end

unauthenticated  do
  root "pages#home", as: :unauthenticated_user
end

您可以使用下面的hacks实现相同的功能,而无需使用
design\u scope

authenticated :patient do
  root to: "patients#show", as: :authenticated_patient
end

authenticated :pharmacist  do
  root "pharmacists#index", as: :authenticated_pharmacist
end

unauthenticated  do
  root "pages#home", as: :unauthenticated_user
end

好吧,你的路线必须是休息满的,这也意味着它们是无状态的(意味着它们必须工作,无论是经过身份验证的患者还是未经身份验证的患者),你唯一能控制的是它们在控制器中的行为。或者换句话说,可以将一个根指向“patient”,另一个根指向“pharmacist”(主机/患者和主机/药剂师),但您不能期望这些路径中的任何一个都知道身份验证。我知道这不能回答您的问题,但我认为您的看法是错误的。请尝试使用名称空间或作用域,而不是
authenticated do
-
scope:authenticated do
。这会使您的url看起来像“主机/药剂师/已验证…”问题是,这一级别的“嵌套”“是有点丑吗?你介意给我一个更具体的例子还是一个链接?你的回答让我有点困惑我真的很抱歉让你更加困惑,但我从未见过
authenticated do
语法,所以我只是试着给你一个大概的想法。我想说的是,你的routes.rb文件似乎是一个url,看起来像“GET www.host/patient”,有两种不同的含义(经过身份验证的患者和未经身份验证的患者)。在评估您的会话之前,您的服务器如何知道您要与“GET www.host/patient”这两个选项中的哪一个对话?它不能,这就是为什么它不能工作的原因,除非Deave提供了我不知道的内容。经过身份验证和未经身份验证的是Deave的一部分…Deave_范围与“as”。如果我删除as条款中的1条,它就会起作用。理想情况下,我只需要在登录后将患者发送到仪表板,并将药剂师发送到仪表板,您的路由必须是剩余的,这也意味着它们是无状态的(这意味着无论是经过身份验证的患者还是未经身份验证的患者,它们都必须起作用),您唯一可以控制的是他们在控制器中的行为。或者换句话说,可以将一个根指向“患者”,另一个根指向“药剂师”(主机/患者和主机/药剂师)但是您不能期望这些路径中的任何一条知道身份验证。我知道这并不能回答您的问题,但我认为您的看法是错误的。请尝试使用名称空间或作用域,而不是
authenticated do
-
scope:authenticated do
。这会使您的url看起来像“主机/药剂师/认证…问题是,这一级别的“嵌套”有点难看你介意给我一个更具体的例子或一个链接吗?我对你的回答有点困惑我真的很抱歉我把你弄糊涂了,但我从来没有见过
authenticated do
语法,所以我只是试着给你一个大概的想法。我想说的是,你的routes.rb文件似乎一个类似于“获取www.host/patient”的url,具有两种不同的含义(已验证的患者和未验证的患者)。您的服务器如何知道“获取www.host/patient”这两个url中的哪一个在评估您的会话之前,您想与之交谈吗?它不能,这就是为什么它不能工作,除非Deave提供我不知道的内容。已验证和未验证是Deave的一部分…Deave_范围与“as”相同。如果我删除了其中的一个as条款,它就起作用了。理想情况下,我只需要它将患者发送到他们的仪表板,药剂师在日志记录后发送到仪表板……如果我所说的话甚至没有提示你问题出在哪里,我会非常难过:当我通读时,我突然意识到了Pit,因为我在想这怎么可能不起作用。然后我看了看我在未经验证的根上写as的方式,然后对自己说,怎么可能两者都是“as”走同样的路。谢谢你的帮助。嗯……如果我说的话没有告诉你问题出在哪里,我会很难过:当我通读的时候,我突然意识到了这个问题,因为我想这怎么可能不起作用。然后我看着我在未经验证的根上写as的方式,然后想自己怎么可以同时写“as”走同样的路。谢谢你的帮助。