Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4 - Acronym Controller Within a Namespace Giving 'Uninitialized Constant' Error

I'm trying to create this controller in Rails 4:

Admin::EDMsController

In my initializers/inflections.rb file, I've defined the acronym:

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

My routes.rb file has:

namespace :admin do
  # ... some other resources ...
  resources :edms
end

And my controller is defined as follows in app/controllers/admin/edms_controller.rb:

class Admin::EDMsController < Admin::AdminController
end

When I try to access /admin/edms, I get the following error:

uninitialized constant Admin::EDMsController

What I've found so far:

  • If I rename it to Admin::EdmsController and remove the inflection definitions (i.e. everything the same except not an acronym), it works
  • If I move the whole thing out of the admin namespace into the root of my app (i.e. EDMsController, accessed via /edms), it works

It's also worth noting that I have other controllers and resources in the admin namespace which work correctly.

So it seems I can have an acronym controller, or a controller within a namespace, but not both.

Any suggestions?

like image 507
Cam Price-Austin Avatar asked Jul 04 '14 01:07

Cam Price-Austin


1 Answers

Looks like this is a bug that was fixed in 4.2: https://github.com/rails/rails/pull/14146

If you can't upgrade, this should work:

irb(main):001:0> ActiveSupport::Inflector.inflections(:en) { |inflect| inflect.acronym 'Admin/EDMs' }
=> /Admin\/EDMs/
irb(main):002:0> 'Admin::EDMsController'.underscore
=> "admin/edms_controller"
like image 124
dougo Avatar answered Sep 19 '22 02:09

dougo