program story

Rails의 단수 또는 복수 컨트롤러 및 헬퍼 이름

inputbox 2020. 7. 30. 10:23
반응형

Rails의 단수 또는 복수 컨트롤러 및 헬퍼 이름


컨트롤러 및 도우미에 단일 이름을 사용하는 데 단점이 있습니까? 아무것도 이것에 의존하지 않는 것 같습니다. 적어도 내 제한된 실험에 따르면, 도우미가 해당 컨트롤러와 같은 단수형과 복수형에 대해 동일한 선택을 할 필요가없는 것 같습니다. 그게 사실입니까?


확실히 복수 .

편안한 라우팅과 단일 컨트롤러

제어 장치:

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

컨트롤러에 복수 이름을 사용하는 것은 단지 관례입니다.

복수 이름은 일반적으로보다 자연스럽게 들리지만 (특히 특정 모델에 직접 연결된 컨트롤러의 경우 : 사용자-> 사용자 등) 원하는대로 사용할 수 있습니다.

도우미의 경우 모든 도우미가 기본적으로 모든 컨트롤러에 제공되므로 기술적으로 도우미 이름을 지정하는 방법은 중요하지 않습니다. 컨트롤러의 도우미 기능을 컨트롤러와 동일한 이름의 도우미에서 유지하는 것은 또 다른 규칙입니다.


모델은 User와 같은 단일 객체를 참조하기 때문에 특이합니다. 컨트롤러는 Users 컬렉션의 컨트롤 (메서드)이므로 복수형입니다. 라우트 이름을 지정하는 방법은 모두 해당 개발자에게 있습니다. 웹 요청의 URL이 단수 또는 복수라는 사용자 불만을 제기 한 적이 없습니다. 최종 결과는 품질 페이지 표시 또는 최종 사용자에 대한 API 요청을 제공하면서 현재 및 향후 기고자에게 공통된 규칙을 유지합니다.


Rails 안내서에 매우 완전한 설명이 있습니다 : http://edgeguides.rubyonrails.org/routing.html#resource-routing-the-rails-default


런타임 동안 해당 모델의 인스턴스가 하나 이상 존재할 수 있는지 여부에 관계없이 하나의 컨트롤러가 하나의 모델을 처리하는 것이 Rails 규칙입니다. 그러나 컨트롤러 (및 관련보기) 중 일부가 특정 모델과 관련되지 않고보다 복잡한 기능 세트를 처리하는 Rails 애플리케이션이있을 수 있습니다. 이 경우 자동 복수형은 의미가 없습니다.

현재 작업중 인 Rails 응용 프로그램 이이 범주에 적합하며 Rails는 한 장소에서 단수로 정의한 식별자가 다른 장소에서 복수 형태로 사용되기를 기대합니다. 예를 들어 다음과 같이 정의하고 싶습니다 config/routes.rb.

  resource :dashboard, :only => [:show]

그런 다음 컨트롤러 DashboardController가 응용 프로그램의 특정 측면에 대한 요약 정보를 표시하고 둘 이상의 데이터베이스 테이블에서 정보를 수집하기를 원합니다 . 따라서 여기에서는 Dashboard응용 프로그램의 모델을 나타내지 않으며 컨트롤러 이름을 갖는 것이 이상합니다 DashboardsController.

나는 이 답변 에서 자동 다원화의 자극에 대한 좋은 해결책을 찾았다 . 간단히 말해서, 파일을 편집 config/initializers/inflections.rb하고 자동으로 원하지 않는 단어를이 정의에 추가하십시오.

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w( dashboard foo bar baz )
end

컨트롤러 이름에 단수를 사용할 때 기분이 좋아집니다.


컨트롤러가 리소스라면 복수 여야합니다 ...

예를 들어

제어 장치

articles_controller.rb

모델

article.rb

그러나 해당 모델이없는 경우 단일 컨트롤러 이름을 사용할 수 있습니다

welcome_controller.rb

Rails 에서 컨트롤러의 이름 지정 규칙은 컨트롤러 이름 에서 마지막 단어의 복수화선호 하지만 엄격하게 요구되지는 않습니다 (예 :)ApplicationController .

예를 들어, ClientsController바람직하다 ClientController, SiteAdminsController것이 바람직하다 SiteAdminControlleR 또는 SitesAdminsController등등.

Following this convention will allow you to use the default route generators (e.g. resources, etc) without needing to qualify each :path or :controller, and will keep URL and path helpers' usage consistent throughout your application.

Ref: Controller Naming Convention-Rails Doc


Using plurals just sounds better, and then if you have a controller that handles a singular resourse, ie user, then you can still name the url /user.

With helpers there is often no need to have a helper for every controller, and often there will be helper methods you can use ascorss multiple controllers and rather litter them all through your application helper you could put them in custom helpers instead like eg layout_helper or any other well named file.

참고URL : https://stackoverflow.com/questions/646951/singular-or-plural-controller-and-helper-names-in-rails

반응형