리소스와 리소스의 차이!
resources 메서드
7개의 기본 컨트롤러 동작 이름을 자동으로 생성하는 루트 방법!
얼마나 편한데
posts표에 대한 트위터,users표에 대한 사용자, 즉 표에 저장된 기록을 자원이라고 합니다!
다음 예시의 설명에서posts자원에 대해 기본 7개의 동작을 요청하는 경로를 설정합니다.
config/routes.rb
Rails.application.routes.draw do
resources :posts
end
터미널에서 라크 루트 명령을 실행하면 다음과 같이 7개의 루트가 설정되어 있는지 확인할 수 있습니다.Prefix Verb URI Pattern Controller#Action
posts GET /posts(.:format) posts#index
POST /posts(.:format) posts#create
new_post GET /posts/new(.:format) posts#new
edit_post GET /posts/:id/edit(.:format) posts#edit
post GET /posts/:id(.:format) posts#show
PATCH /posts/:id(.:format) posts#update
PUT /posts/:id(.:format) posts#update
DELETE /posts/:id(.:format) posts#destroy
resource 메서드
resource 방법은 컨트롤러의 7개 동작에 색인과 id를 포함하는 경로를 생성하지 않습니다!
id가 필요 없는 상황에서 show,edit 동작을 실행하면 유효합니다!
다음 예에서posts라는 자원에 대해 index를 요청하고 id 경로를 제거하는 나머지 동작의 경로를 설정합니다.
config/routes.rb
Rails.application.routes.draw do
resource :posts
end
터미널에서 라크 루트 명령을 실행하면 다음과 같이 7개의 루트가 설정되어 있는지 확인할 수 있습니다.Prefix Verb URI Pattern Controller#Action
new_posts GET /posts/new(.:format) posts#new
edit_posts GET /posts/edit(.:format) posts#edit
posts GET /posts(.:format) posts#show
PATCH /posts(.:format) posts#update
PUT /posts(.:format) posts#update
DELETE /posts(.:format) posts#destroy
POST /posts(.:format) posts#create
이상은!장면에 따라 구분해서 사용하면 아주 편리해요!
마음에 드는 점이 있으면 마음대로 메시지를 남겨 주세요!
경청해 주셔서 감사합니다.
Reference
이 문제에 관하여(리소스와 리소스의 차이!), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/wacker8818/items/1ba526fcbc73e065a511텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)