리소스와 리소스의 차이!

2942 단어 RubyRails
응용 프로그램 개발에서 루트를 설정하는 토대에서 양자의 차이에 대해 매우 신경을 쓰기 때문에 나는 그것을 비망록에 포함하여 총결하고 싶다.

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

이상은!
장면에 따라 구분해서 사용하면 아주 편리해요!
마음에 드는 점이 있으면 마음대로 메시지를 남겨 주세요!
경청해 주셔서 감사합니다.

좋은 웹페이지 즐겨찾기