开发者

I created a scaffold in rails called Cove. In the controller, the instance variable is automatically named @cofe. Why is that happening?

开发者 https://www.devze.com 2023-02-06 12:50 出处:网络
Here are the steps I did. In command开发者_如何学运维 line: rails new lcdemo rails generate scaffold Cove title:string

Here are the steps I did. In command开发者_如何学运维 line:

rails new lcdemo rails generate scaffold Cove title:string

(created an entry in seeds.rb)

rake db:migrate rake db:seed

In the controller and view files, instance variables are named cofe instead of cove.


The reason

"cove".pluralize
=> "coves"
"coves".singularize
=> "cofe"

The solution

You can edit your config/initializers/inflections.rb file to create a custom inflection to teach Rails that the singular of "coves" is "cove".

inflect.irregular 'cove', 'coves'


That'll be the Inflector working its magic. It looks like it's pluralizing "Cove" to "coves" in order to make the table name, and then singularizing "coves" (by analogy with "wolves", "knives" etc) to "cofe".

I don't know if it'll work, but you could try adding the following to config/initializers/inflections.rb before you run the scaffold generator?

inflect.plural "cove", "coves"
inflect.singular "coves", "cove"
0

精彩评论

暂无评论...
验证码 换一张
取 消