티스토리 뷰
[Rails][Web API] PUT & PATCH, DELETE 요청 처리하기
이전 글에서 API 제작 시 POST 메서드를 처리하는 법을 배웠습니다. 이번 글에서는 PUT&PATCH와 DELETE 요청을 처리하는 법을 다뤄보겠습니다. 이전 글을 읽고 이번 글을 따라오시길 바랍니다.
PUT & PATCH 처리하기
1 2 3 4 | # app/models/episode.rb class Episode < ActiveRecord::Base validates :title, length: { mininum: 10 } end | cs |
위와 같이 episode 모델이 정의되어있다고 가정합니다. 해당 컨트롤러에서 update 메서드를 다음과 같이 처리하면 됩니다.
1 2 3 4 5 6 7 8 9 10 11 12 | #app/controller/episodes_controller.rb class EpisodesController < ApplicationController def update episode = Episode.find(params[:id]) if episode.update(episode_params) render json: episode, status: 200 else render json: episode.errors, status: 422 end end end | cs |
DELETE 처리하기
1 2 3 4 5 6 7 8 9 | #app/controller/episodes_controller.rb class EpisodesController < ApplicationController def destroy episode = Episode.find(params[:id]) episode.destroy head 204 end end | cs |
POST 때와 내용이 유사하기 때문에 별도의 설명은 하지 않겠습니다. 자세한 설명은 POST 글을 참고하여 주세요.
'Ruby&Rails > Rails' 카테고리의 다른 글
| [Rails 4] Self-Referential Association (has_many) (0) | 2015.08.10 |
|---|---|
| [Rails][Web API] Post 요청 처리하기 (0) | 2015.07.11 |
| [Rails] 웹 API를 위한 JSON, XML 응답 (0) | 2015.07.07 |
| [Rails] Rails에서의 subdomain 설정하기 (0) | 2015.07.06 |
| [Rails] Routing에서 concern 을 이용한 중복 resources 관리 (0) | 2015.06.29 |
댓글
Copyright ⓒ 2018 moneystory.blog. All rights reserved.
