Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override Rails controller routing with capital letters in model name

I want to create a model in rails:

rails generate model ABCThing

So this will create a table, abc_things. Great. The problem comes with the controller and routing. I want my controller to be:

class ABCThingsController < ApplicationController
end

However, after adding in the routes.rb

resources :abc_things, :only => [:index]

and creating the corresponding index view, i get the following error in the browser:

Expected /app/controllers/abc_things_controller.rb to define AbcThingsController

The problem is easy to see ("ABCThings".tableize.classify => "AbcThing"), but i'm not so sure how to fix it. I want to override rails default routing from the view to the controller, but am not sure how.

Would appreciate any help (and suggestions for a better question title!)

like image 312
rwb Avatar asked Sep 20 '12 08:09

rwb


2 Answers

I had this issue and after trying all of the above solutions; was able to fix my problem using the inflector.

In my case the issue was that TLA::ThingsController was being resolved as Tla::ThingsController

putting the following in my initializers folder fixed it

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.acronym 'TLA'
end
like image 50
Tyrone Wilson Avatar answered Sep 17 '22 06:09

Tyrone Wilson


You should set custom controller name, in routes.rb:

resources :abc_things, :only => [:index], :controller => "ABCThings"
like image 43
byterussian Avatar answered Sep 17 '22 06:09

byterussian