【问题标题】:Rails 6 Routes SubdomainRails 6 路由子域
【发布时间】:2020-07-21 19:19:11
【问题描述】:

我正在为我的 Rails 项目(Rails 6、Ruby 2.6.6)设置子域系统。

我的子域系统使用以下方法可以正常工作

constraints subdomain: 'chef' do
  devise_for :chefs, controllers: { registrations: 'chefs/registrations' },
  resources :chefs do
    resources :chef_profiles
  end
end

但我想更改 URI 的呈现方式。

目前,如果我想查看 chef_profile - 路线是 chef.domain.com/chefs/:id/chef_profile/:id 我想要的是 chef.domain.com/chef_name

关于如何改变嵌套工作方式的任何想法?尝试做as: :...,但不知道在as 部分放什么。

提前感谢您的帮助!

【问题讨论】:

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


    【解决方案1】:

    不劫持其余路线的最简单方法是使用 get 匹配器:

    constraints subdomain: 'chef' do 
      get "/:chef_name", to: "chef_profiles#show", chef_name: /-?[a-z0-9][a-z0-9\-\_]*/i
    end
    

    您可能需要调整 chef_name 正则表达式以适合您的模式。

    【讨论】:

      猜你喜欢
      • 2013-12-04
      • 1970-01-01
      • 1970-01-01
      • 2011-04-09
      • 2014-02-27
      • 2012-07-30
      • 2011-11-01
      • 1970-01-01
      相关资源
      最近更新 更多