次æRailsãPATCHã¡ã½ãããæ¡ç¨
å æãRailsã®ããã°ã«ãããªè¨äºãè¼ãã¾ããã
- http://weblog.rubyonrails.org/2012/2/25/edge-rails-patch-is-the-new-primary-http-method-for-updates/
ãªããå½åã¨URLãå¤ãã£ã¦ããããã§ãããã¯ãã¼ã¯ã¯ Edge Rails: PATCH is the new primary HTTP method for updates | Riding Rails ã®ã»ããå¤ãã§ããï¼ããããã£ã¡ã¯ããã°ã®ã³ã¡ã³ãããã£ã±ãã¤ãã¦ãã¨æããã ãæ¶ãã¦ããâ¦ï¼
ããã¯ç°¡åã«è¨ãã¨ã
PUTã¯åªç
Railsã®routes.rbã§
resources :users
ã¨æ¸ãã¨ãããã¤ãã®ã«ã¼ãã£ã³ã°ãçæããã¾ããããã®ä¸ã«
PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
ã¨ãããã®ãã§ãã¾ãããªã½ã¼ã¹æ´æ°ã®ãããHTTP PUTã¡ã½ããã使ãã¨ã¢ã¯ã·ã§ã³ã¡ã½ããupdateãå¼ã³åºãããã¨ãããã®ã§ãã
âRESTfulâã§ä¸è¦åé¡ãªãããã«è¦ãã¾ãããããããPUTã¡ã½ããã¯ã©ãããæ§è³ªã®ãã®ãªã®ã§ãããããããã¯RFC2616ã«è¦å®ããã¦ãã¾ãã
http://www.studyinghttp.net/cgi-bin/rfc.cgi?2616#Sec9.1.29.1.2 åªç{idempotent} ãªã¡ã½ãã
ã¡ã½ããã¯ã(ã¨ã©ã¼ãæéåãçºè¡ã¨ã¯å¥ã«) åä¸ã®ãªã¯ã¨ã¹ãã® N > 0 ã®å¯ä½ç¨ãåä¸ã®ãªã¯ã¨ã¹ãã«ããããã®ã¨åãã§ãããããªéã«ã¯ "åªç{idempotence}" ã®æ§è³ªãæã¤äºãã§ããã GET, HEAD, PUT, DELETE åã¡ã½ããã¯ãã®æ§è³ªãå ±æããã
PUTã¯åªçãªã¡ã½ããã¨ãããã¨ã§ãããåªçï¼ã¹ãã¨ãï¼ãã¨ããã®ã¯æ°å¦çãªç¨èªã§å°ãé£ããã§ããããã®èª¬æãããããããã§ãã
ä¾ãã°ããã©ã¦ã¶ãããã HTML ãã¡ã¤ã«ã« GET ãªã¯ã¨ã¹ããè¡ã£ãã¨ãã¾ãã
http://www.studyinghttp.net/method
ãã®å ´åããã® HTML ææ¸ããã©ã¦ã¶ã«åºåãããã§ãããã
ãã®æãããä¸åº¦åããã¡ã¤ã«ã«åã GET ãªã¯ã¨ã¹ããè¡ã£ãã¨ãããããã®çµæã¯ã©ããªãã§ãããã?
ãã®çµæã¯ãå½ç¶å ç¨ã¨åãææ¸ããåãããã«ãã©ã¦ã¶ã«åºåãããäºã§ãããã
ä¸è¬ã«ãåããã¼ã¿ã«ã¤ãã¦ä½åº¦æä½ãè¡ãªããã¦ããåãçµæã«ãªããããªæä½ãã®äºãåªçãªæä½ã¨è¨ãã¾ãã
ããªãã¡ããªã½ã¼ã¹ãåå¾ããããã® GETããªã½ã¼ã¹ã«é¢ããããããåå¾ããããã® HEADããªã½ã¼ã¹ãã¢ãããã¼ãããããã® PUTããªã½ã¼ã¹ãåé¤ããããã® DELETE çã¯ãåãã¡ã½ãããä½åº¦ç¹°ãè¿ãã¦ãåæ§ãªçµæãè¿ãã¯ããªã®ã§ããããã¯åªçãªã¡ã½ããã§ããã¨è¨ãã¾ãã
åªçã ã¨ä½ãããããã®ãã¨ãããã¨ã§ããããã®ããã大ãã£ã±ã«è¨ãã¨ããªã³ã¯ã»ãã¿ã³ãé£æãã¦ãã ããããã¶ãã¿ãããªæãã§ããä¾ãã°ã¢ãã¤ã«ç°å¢ãªã©ãããã¯ã¼ã¯ãä¸å®å®ã§ã¬ã¹ãã³ã¹ãã¡ããã¨è¿ã£ã¦ããªãã£ãããªã¯ã¨ã¹ããåãä»ãããããã©ããããããªããã¨ããå ´åã§ããåªçãä¿è¨¼ããã¦ããã¡ã½ããã§ããã°ãã ããä¸åº¦ãªã¯ã¨ã¹ããéãã°ããã®ã§ãã
updateã¯åªçï¼
ãã¦ãRailsã®updateã¢ã¯ã·ã§ã³ã¯æ¬å½ã«åªçã§ããããï¼
ããã¯ã¾ããå®è£
ã«ãããããã§ãããå¤ãã®å ´åRailsã®ã¢ãã«ã§ã¯created_at, updated_atã¨ããã¿ã¤ã ã¹ã¿ã³ãæ©è½ãå©ç¨ãã¾ãããã®æ©è½ã§ã¯æ´æ°æ¥æã¯èªåçã«æ¸ãè¾¼ã¾ãã¾ããããã¦ããã®æ¥æã¯GETã§åå¾ã§ãããããªã½ã¼ã¹å
ã®ãã¼ã¿ã¨ãã¦æ®éã«ä½¿ã£ã¦ãã¾ãã
ã¨ãããã¨ã¯ãPUTã2åå®è¡ããããæ¥æãéãããã ããéããã¼ã¿ã«ãªã£ã¦ãã¾ãã¾ãããããåªçãããªãããããå°ã£ããã
åªçã§ãªãæ´æ°ã¯PATCH
ããã§ãæã«ææ¡ããã¦ããHTTPã¡ã½ãããPATCHã§ãã
PATCHã¯ãé¨åæ´æ°ã®ããã®ã¡ã½ããã§ãããåªçã§ãªãã¨è¦å®ããã¦ãã¾ã*1ãã¡ããã©ããããã¨ãããã¨ã§ã次ã®ãã¼ã¸ã§ã³ã®Rails 4ã§ã¯updateã¢ã¯ã·ã§ã³ã¯PATCHã¡ã½ããã«çµã³ã¤ãããã«ãªãã¾ããããã ããäºææ§ãèãã¦ãPUTã¡ã½ããã§ãupdateã¢ã¯ã·ã§ã³ãå¼ã³åºããã¾ããã«ã¼ãã£ã³ã°ã¯
PATCH|PUT /users/:id(.:format) {:action=>"update", :controller=>"users"}
ã¨ãªãã§ãããã
æ¬å½ã«ããã¯é©åãªã®ãã©ãã
説æã¯ä»¥ä¸ã§ãããã®å¤æ´ã¯æ¦ã好æçã«åãæ¢ãããã¦ããããã§ãã
ãã ããä¸è¨ã§ã¯PATCHæ¡ç¨ã®ãã1ã¤ã®çç±ã§ããããªã½ã¼ã¹ã®é¨åæ´æ°ããããã®èª¬æãããã¦çç¥ãã¾ãããããã¯ã¡ãã£ã¨å人çã«æãã¨ãããããããã§ãã
ã¨ããæããªã®ã§ããããã®è©³ç´°ã¯æ¬¡åã«ç¶ãã¨ãããã¨ã§â¦ã
*1:åªçã§ãã£ã¦ã¯ãããªããã¨ããæå³ã§ã¯ãªããåªçã§ããã