REST 란?
REST는 REpresentational State Transfer 의 줄임말이다.
JSON(JavaScript Object Notation) 와는 다른, 네트워크에서 리소스를 어떻게 표기하고 정의하는지를 정하는 아키텍쳐중 하나이다.
리소스는 우리가 페이지에서 표기하거나 처리하고 싶은 모든 데이터를 의미한다.
REST 아키텍쳐는 Http 의 네가지 메소드(GET, PUT, PUSH, DELETE)를 기반으로 한 각종 유니크한 명령어를 문자로 제공해준다.
예를 들어 Rails에서 articles 이라는 테이블에서 새로운 작성페이지를 'GET'으로 불러오고 싶으면:
http://mypage/articles/new
이렇게 쓴다. 물론 new 라는 메소드는 컨드롤러에서 정의가 되어있어야 한다.
더 자세한 정의는 여기에서 찾아볼수 있다.
RESTful URL
Rails에서 REST로 제공되는 url을 알아내는 가장 간단한 방법은 Bash 에서
1 | $ rake routes | cs |
를 치면 된다. 그러면 REST와 관련된 리소스들의 url들이 자동으로 뜬다.
1 2 3 4 5 6 7 8 9 10 11 | Prefix Verb URI Pattern Controller#Action articles_index GET /articles/index(.:format) articles#index new_articles GET /articles/new(.:format) articles#new edit_articles GET /articles/edit(.:format) articles#edit articles GET /articles(.:format) articles#show PATCH /articles(.:format) articles#update PUT /articles(.:format) articles#update DELETE /articles(.:format) articles#destroy POST /articles(.:format) articles#create root GET / articles#index | cs |
또 다른방법으로는 Rails에서 기본적으로 제공하는 Named Helper들이 있다.
HTTP Verb | Path | Controller#Action | Named Helper |
---|---|---|---|
GET | /admin/articles | articles#index | articles_path |
GET | /admin/articles/new | articles#new | new_article_path |
POST | /admin/articles | articles#create | articles_path |
GET | /admin/articles/:id | articles#show | article_path(:id) |
GET | /admin/articles/:id/edit | articles#edit | edit_article_path(:id) |
PATCH/PUT | /admin/articles/:id | articles#update | article_path(:id) |
DELETE | /admin/articles/:id | articles#destroy | article_path(:id) |
Named Helper를 url처럼 사용하면 된다. 여기에서 리소스 이름은 자기가 쓰고 싶은것으로 변경하면 된다
참조:
http://guides.rubyonrails.org/routing.html
http://www.service-architecture.com/articles/web-services/representational_state_transfer_rest.html
https://www.slideshare.net/rmaclean/json-and-rest
http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm
'Web Design > Ruby on Rails(ROR)' 카테고리의 다른 글
Rails db migration 관련 커맨드 (0) | 2017.06.15 |
---|---|
Rails Model의 db에서 지원하는 변수목록 (0) | 2017.06.15 |
Rails MVC(Model-View-Controller) 이름짓기 (0) | 2017.06.12 |
Rails html <form> 자동생성 기능: form_for (0) | 2017.06.09 |