routes.rb をもう一度考えてみた #shibuyarb
Upcoming SlideShare
Loading in...5
×
 

routes.rb をもう一度考えてみた #shibuyarb

on

  • 6,264 views

補足 http://d.hatena.ne.jp/tkawa/20120720/p1

補足 http://d.hatena.ne.jp/tkawa/20120720/p1

Statistics

Views

Total Views
6,264
Views on SlideShare
5,256
Embed Views
1,008

Actions

Likes
7
Downloads
17
Comments
0

5 Embeds 1,008

http://d.hatena.ne.jp 989
https://si0.twimg.com 12
http://us-w1.rockmelt.com 3
https://twimg0-a.akamaihd.net 3
http://tweetedtimes.com 1

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

routes.rb をもう一度考えてみた #shibuyarb routes.rb をもう一度考えてみた #shibuyarb Presentation Transcript

  • routes.rb をもう一度考えてみた @tkawa 2012.7.18 Shibuya.rb
  • @tkawa 川村 徹RESTとかRailsとか書いて るブログhttp://d.hatena.ne.jp/tkawa/ Web認知行動療法 U2plus http://u2plus.jp/
  • routes.rb書いてますか? resources使ってますか?
  • Hoge::Application.routes.draw do  resources :usersend
  • @kennIs it only me who think rails routes.rb is amess? https://twitter.com/kenn/status/184076612504006656@miyagawaconfig/routes.rb goddamnit https://twitter.com/miyagawa/statuses/221405166853828608@jugyoroutes.rb 開くたびに憂鬱になる https://twitter.com/jugyo/status/14690985107
  • routes.rb sucks?
  • routes.rb の悪いところを考えてみた
  • I.resources が直感的でない
  • 基本7アクション index show new edit create update destroy
  • resources :users$ rake routes users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#newedit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy
  • resources :users GET POST PUT DELETE /users index create - -/users/:id show - update destroy GET /users/new → new GET /users/:id/edit → edit
  • II. Convention overConfiguration じゃない
  • 7アクションが「規約」ならresources も書かなくてもいい のでは?
  • Conventional Routeshttps://github.com/tkawa/conventional_routes
  • http://d.hatena.ne.jp/tkawa/20120407/p1
  • Conventional Routesapp/controllers/admin/special/licenses_controller.rbapp/controllers/admin/users_controller.rbapp/controllers/users_controller.rb namespace :admin do   namespace :special do     resources :licenses   end   resources :users end resources :users
  • Conventional Routes• routes.rb に何も書かなくても、 コントローラが存在すれば、自動的に resources とみなす• ディレクトリは namespace• もしコントローラの中まで見れば、 Sinatra的なこともできそう
  • Sinatra的なことをもうやってる人がいた! (しかも2年前)
  • Routes are unnecessary configuration.The seven standard controller actions are legacy.Become intimate with your URLs – don’t abstractthem away.Decrease the distance between thought andimplementation.Let the controller do its job. http://peepcode.com/blog/2010/rethinking-rails-3-routes
  • His idea# Class method and HTTP-style methods.class ReportsController < ApplicationController  before_filter :authenticate  resource "/reports(/:id)"  get(:collection) do  end  get(:member) do |id|  end  put(:member) do |id|  endend
  • Astairehttps://github.com/pehrlich/astaire
  • Astaire (like Sinatra)# Class method and HTTP-style methods.class ReportsController < ApplicationController  before_filter :authenticate  get "/reports" do  end  get "/reports/:id" do  end  put "/reports/:id" do  endend
  • 2年も経ってるのに反応薄いし、やっぱりいまいち?
  • Sinatra式の問題点• Viewの命名規約が別途必要 {view}.html.erb どういう名前にする?• resources のパターンを活かせない GET POST PUT DELETE /reports index create - -/reports/:id show - update destroy
  • いろいろ考えたけど• 今の routes.rb はそれなりにベター• 基本7アクションを残すなら、 match (get, post, put, delete) はむしろ書かなく てすむようにしたい• とにかく resources (resource) にすることで、 routes.rb はシンプルになる。REST的にもOK
  • 例 resources :messages, except: [ :new ] do post :trash, :restore, on: :member resources :image_attachments, only: :index endhttps://github.com/rails/routing_concerns
  • resources :messages, except: [ :new ] do post :trash, :restore, on: :member resources :image_attachments, only: :indexendresources :messages, except: [ :new ] do member do put :trashed delete :trashed end resources :image_attachments, only: :indexend
  • resources :messages, except: [ :new ] do member do put :trashed delete :trashed end resources :image_attachments, only: :index end ※ちょっと意味が変わる resources :messages, except: [ :new ] do resource :trashed, only: [:create, :destroy] resources :image_attachments, only: :index end# “partial” option does not exist in factresources :messages, except: [ :new ], partial: [:trashed] do resources :image_attachments, only: :indexend
  • RailsにおけるRESTfulなURL設計勉強会 Sendagaya.rb #12 7/23 (月) 19:00- http://www.zusaar.com/event/324057