找不到带下划线的路线或者没有正确对待它
我在路线中有这个:
Rails.application.routes.draw do namespace :api do namespace :v3_4 do # .....
和控制器app/controllers/api/v3_4/base_controller
module Api module V3_4 class BaseController < ApplicationController # ...... end end end
和app/controllers/api/v3_4/another_controller
module Api module V3_4 class AnotherController < ApplicationController end end end
耙路线:
Prefix Verb URI Pattern Controller#Action api_v3_4_test GET /api/v3_4/test(.:format) api/v3_4/base#test api_v3_4_one GET|OPTIONS /api/v3_4/one(.:format) api/v3_4/another#one api_v3_4 GET|OPTIONS /api/v3_4/two/:id(.:format) api/v3_4/another#two
然而对于这个请求,我得到Routing Error Uninit Constant uninitialized constant Api::V34
请注意,错误消息中没有下划线。 但我的项目根本没有V34线,也没有v34,只有v3_4和V3_4
Rails使_
变为单词分隔符,因此它搜索Api :: V34,您可以通过编辑config/initializers/inflections.rb
来改变该行为:
ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'V3_4' end
此外,如果您想将Api
名称空间更改为API
,因为它是首字母缩略词,您也可以在那里执行:
ActiveSupport::Inflector.inflections(:en) do |inflect| inflect.acronym 'V3_4' inflect.acronym 'API' end
更多信息: http : //api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html
Ruby-Style-Guide帮助我澄清了这些问题。 请参阅命名部分。