Rails scaffolding pluralisation is incorrect for “cafe”

天大地大妈咪最大 提交于 2020-01-12 02:26:13

问题


I want to create a cafe and a cave controller.

When I try to create my cafe using rails scaffolding, via the command

rails g scaffold cafe name:string

It is deriving the plural form of "cafe" as "caves", which means I can't make my caves controller since the name is already used.

How can I make rails use the correct pluralisation?


回答1:


You can create your own inflections.

Add this to your config/initializers/inflections.rb

    ActiveSupport::Inflector.inflections do |inflect|
        inflect.plural "cafe", "cafes"
    end

(Restart your server after making this change. This is not required for the scaffolding command itself but it will be required when you want to actually view/use the code)

Now when you run rails g scaffold cafe you'll get:

...
app/views/cafes
      create      app/views/cafes/index.html.erb
      create      app/views/cafes/edit.html.erb
      create      app/views/cafes/show.html.erb
      create      app/views/cafes/new.html.erb
      create      app/views/cafes/_form.html.erb
etc

This may help you: http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-inflections



来源:https://stackoverflow.com/questions/10861635/rails-scaffolding-pluralisation-is-incorrect-for-cafe

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