【问题标题】:Rails routing using devise and subdomain constraint使用设计和子域约束的​​ Rails 路由
【发布时间】:2017-09-10 07:19:26
【问题描述】:

我正在尝试路由 root 以在 subdomain 约束下设计登录路径。

我的config/routes.rb 看起来像这样

Rails.application.routes.draw 
  constraints subdomain: 'admin' do
    devise_scope :admin do
      root to: 'devise/sessions#new'

      # here I override devise routes
    end
  end

  root to: 'pages#homepage'

  # rest of the routes  
end

我收到错误Could not find devise mapping for path "/".

关于如何在具有设计范围的子域中路由到根路径的任何建议?

谢谢

【问题讨论】:

  • 是这样工作的吗:root to: 'devise/sessions#new', constraints: { subdomain: 'admin' }
  • 感谢您的跟进。我仍然需要将它放在devise_scope 块内,从而覆盖默认设计路线。为此,我将其放在 constraint 块中。不知何故,我仍然无法从那里访问devise 控制器。

标签: ruby-on-rails ruby devise routes ruby-on-rails-5


【解决方案1】:

要向路由添加身份验证约束,请使用设计authenticated 方法:

Rails.application.routes.draw 
  constraints subdomain: 'admin' do
    authenticated :admin do
      # the root page for authenticated users
      root 'admin#dashboard', as: :authenticated_root
    end
    root to: 'devise/sessions#new'
  end

  # this is for no subdomain
  root to: 'pages#homepage'
end

【讨论】:

  • devise_scope 实际上只是 scope 的设计版本,它在设计路径中安装路由。 devise_scope :users 将在 /users 中挂载路径。
  • 感谢您的回复。我们只能从devise_scope 内部路由来设计控制器。我尝试使用unauthenticated 路由到root,但它仍然显示相同的错误消息。有没有办法可以devise_scope 路由并覆盖默认值,同时还指定root to: 'devise/sessions#new'
猜你喜欢
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 2014-11-05
  • 2013-09-25
  • 1970-01-01
  • 2021-05-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多