对于“cafe",Rails 脚手架复数形式不正确 [英] Rails scaffolding pluralisation is incorrect for "cafe"

查看:42
本文介绍了对于“cafe",Rails 脚手架复数形式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 cafe 和一个 cave 控制器.

I want to create a cafe and a cave controller.

当我尝试使用 rails 脚手架创建我的 cafe 时,通过命令

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

rails g scaffold cafe name:string

rails g scaffold cafe name:string

它将cafe"的复数形式派生为caves",这意味着我无法制作我的 caves 控制器,因为该名称已被使用.

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.

如何让 rails 使用正确的复数形式?

How can I make rails use the correct pluralisation?

推荐答案

您可以创建自己的变化.

You can create your own inflections.

将此添加到您的config/initializers/inflections.rb

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

(进行此更改后重新启动您的服务器.这对于脚手架命令本身不是必需的,但是当您想要实际查看/使用代码时需要它)

现在当你运行 rails g scaffold cafe 你会得到:

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

这可能对您有所帮助:http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-inflections

这篇关于对于“cafe",Rails 脚手架复数形式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆