Override Rails controller routing with capital letters in model name

浪子不回头ぞ 提交于 2019-12-03 02:36:15

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

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

resources :abc_things, :only => [:index], :controller => "ABCThings"

This may have changed with Ruby at some point, but for naming classes with multiple caps in a row (acronyms or initialisms), you no longer need to include the underscore in the file name.

# abc_thing.rb

could contain

class ABCThing

  def hello
    puts "Hello World"
  end

end

or

class AbcThing

  def hello
    puts "Hello World"
  end

end

When you run command

rails generate model ABCThings

It will generate model and not a controller. If you want both model and controller use following

rails generate scaffold ABCThings

I think you are not generate controller by using rails command and hence problem was occured to generate controller use following command

rails generate controller ABCThings

and you can /app/controllers/abc_things_controller.rb as follows

class AbcThingsController < ApplicationController
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!