rails 脚手架不是复数生成

rails scaffold not plural generate

我想生成一个脚手架控制器、视图和助手,其名称不是复数,而是单数。但是,我得到了如下所示的复数命名文件。我知道它们基于 rails 约定,但我只想创建一个字典页面。

$ rails generate scaffold_controller dictionary
  create  app/controllers/dictionaries_controller.rb
  invoke  erb
  create    app/views/dictionaries
  create    app/views/dictionaries/index.html.erb
  create    app/views/dictionaries/edit.html.erb
  create    app/views/dictionaries/show.html.erb
  create    app/views/dictionaries/new.html.erb
  create    app/views/dictionaries/_form.html.erb
  invoke  helper
  create    app/helpers/dictionaries_helper.rb

所以,我的问题是 "Are there any better ways or the shortest way to generate a singular named controller/view/helper files by command?"

我要的就在下面。我想知道我是否应该手动制作文件?

  app/controllers/dictionary_controller.rb
  app/views/dictionary/index.html.erb
  app/helpers/dictionary_helper.rb

有一种方法可以做到这一点,您可以使用 inflections 来记录此类异常:

config/initializers/inflections.rb

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "dictionary"
end

不用弄乱 rails 中的任何东西,您可以使用 resource 而不是 resources more about that here

来设置路由

您仍将使用复数控制器名称(如果您需要处理),但 URL 将是单数,并且您不会像复数 resources 那样拥有所有操作( link 中的更多信息),如果您只想要一页,则使用 only: :show

进行限制
resource :dictionary, only: :show

那么您甚至不需要控制器,只需创建文件 dictionaries/show.html.haml(或 erb)就可以了

http://example.com/dictionary