Alan Coromano
Alan Coromano

Reputation: 26018

Can't find a route with an underscore or doesn't treat it properly

I have this in routes:

Rails.application.routes.draw do
  namespace :api do
    namespace :v3_4 do
      # .....

And the controller app/controllers/api/v3_4/base_controller

module Api
  module V3_4
    class BaseController < ApplicationController
      # ......
    end
  end
end

And app/controllers/api/v3_4/another_controller

module Api
  module V3_4
    class AnotherController < ApplicationController

    end
  end

end

rake routes:

          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

And yet for this request I get Routing Error Uninit Constant uninitialized constant Api::V34

Note there's no underscore in the error message. But my project there's no line V34 at all, neither v34, only v3_4 and V3_4

Upvotes: 3

Views: 1051

Answers (2)

Max Filippov
Max Filippov

Reputation: 2082

Rails inflects _ to be a word separator, so it searches for Api::V34 you can change that behavior by editing config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'V3_4'
end

Moreover, if you want to change Api namespace to API, since it's an acronym, you can do it there as well:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.acronym 'V3_4'
  inflect.acronym 'API'
end

More info: http://api.rubyonrails.org/classes/ActiveSupport/Inflector/Inflections.html

Upvotes: 6

guitarman
guitarman

Reputation: 3310

The Ruby-Style-Guide helped me a lot to clarify these questions. Please see the naming section.

Upvotes: 0

Related Questions