中的单数或复数控制器和助手名称

中的单数或复数控制器和助手名称

本文介绍了Rails 中的单数或复数控制器和助手名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对控制器和助手使用单数名称有什么缺点吗?似乎没有什么依赖于此.至少根据我有限的实验,助手似乎不必像他们相应的控制器一样对单数和复数做出相同的选择.是真的吗?

Is there any disadvantage to using singular names for controllers and helpers? Nothing seems to rely on this. It even seems helpers don't have to make the same choice about singular vs. plural as their corresponding controllers, at least according to my limited experimentation. Is that true?

推荐答案

绝对复数.

控制器:

dog_controller.rb

路线:

map.resources :dogs  # => blows up
map.resources :dog  # is ok, but...
dogs_path # => blows up
dog_path  # => ok

使用复数控制器

控制器:

dogs_controller.rb

路线:

map.resources :dogs
dogs_path # => ok
dog_path # => ok

rails generate controller --help 有多个例子:

Example:
`rails generate controller CreditCards open debit credit close`

CreditCards controller with URLs like /credit_cards/debit.
    Controller: app/controllers/credit_cards_controller.rb
    Test:       test/controllers/credit_cards_controller_test.rb
    Views:      app/views/credit_cards/debit.html.erb [...]
    Helper:     app/helpers/credit_cards_helper.rb

这篇关于Rails 中的单数或复数控制器和助手名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 03:23