Rails中不区分大小写的路由

时间:2017-09-13 14:34:25

标签: ruby-on-rails

如何将路线的一部分资本化?例如,我有一条路线scim/v2/user,但我希望它是scim/v2/User(用户大写)。如何在使用resource时实现此目的。

路由文件:

namespace :scim, defaults: { format: :json } do
  namespace :v2 do
    resource :user, only: [:create, :update, :show]
  end
end

当我运行$rake routes时,我明白了:

                       scim_v2_user POST               /scim/v2/user(.:format)                                                           scim/v2/users#create {:format=>:json}
                                 GET                /scim/v2/user(.:format)                                                           scim/v2/users#show {:format=>:json}
                                 PATCH              /scim/v2/user(.:format)                                                           scim/v2/users#update {:format=>:json}
                                 PUT                /scim/v2/user(.:format)                                                           scim/v2/users#update {:format=>:json}

我希望路线为/scim/v2/User或让它们保持不变,但可以将/scim/v2/User映射到/scim/v2/user

2 个答案:

答案 0 :(得分:1)

默认情况下,resource需要资源名称和控制器之间的直接映射,但您可以简化使用大写资源名称并手动指定控制器来解决此问题:

  namespace :scim, defaults: { format: :json } do
    namespace :v2 do
      resource :User, :controller => 'users', only: [:create, :update, :show]
    end
  end

生成

      Prefix Verb  URI Pattern             Controller#Action
scim_v2_User GET   /scim/v2/User(.:format) scim/v2/users#show {:format=>:json}
             PATCH /scim/v2/User(.:format) scim/v2/users#update {:format=>:json}
             PUT   /scim/v2/User(.:format) scim/v2/users#update {:format=>:json}
             POST  /scim/v2/User(.:format) scim/v2/users#create {:format=>:json}

答案 1 :(得分:0)

我能够通过手动指定pathcontroller来解决此问题。我指定path应为用户(大写)。以下是我的路线文件中的代码:

  namespace :scim, defaults: { format: :json } do
    namespace :v2 do
      resources :user,
        path: "Users",
        controller: "users",
        only: [:create, :update, :index, :show]
    end
  end