routes.rb をもう一度考えてみた #shibuyarb
DESCRIPTION
補足 http://d.hatena.ne.jp/tkawa/20120720/p1TRANSCRIPT
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
@kenn
Is it only me who think rails routes.rb is a mess?
@miyagawa
config/routes.rb goddamnit
https://twitter.com/kenn/status/184076612504006656
https://twitter.com/miyagawa/statuses/221405166853828608
@jugyo
routes.rb 開くたびに憂鬱になる
https://twitter.com/jugyo/status/14690985107
routes.rb sucks?
routes.rb の悪いところを考えてみた
I.resources が直感的でない
基本7アクションindex
show
new
edit
create
update
destroy
$ 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 → newGET /users/:id/edit → edit
resources :users
II.Convention over
Configuration じゃない
7アクションが「規約」なら resources も書かなくてもいい
のでは?
Conventional Routes
https://github.com/tkawa/conventional_routes
http://d.hatena.ne.jp/tkawa/20120407/p1
namespace :admin do namespace :special do resources :licenses end resources :users end resources :users
app/controllers/admin/special/licenses_controller.rbapp/controllers/admin/users_controller.rbapp/controllers/users_controller.rb
Conventional Routes
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 abstract them away.
Decrease the distance between thought and implementation.
Let the controller do its job.
http://peepcode.com/blog/2010/rethinking-rails-3-routes
# 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| end
end
His idea
Astaire
https://github.com/pehrlich/astaire
# 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 end
end
Astaire (like Sinatra)
2年も経ってるのに反応薄いし、やっぱりいまいち?
• Viewの命名規約が別途必要{view}.html.erb どういう名前にする?
• resources のパターンを活かせない
GET POST PUT DELETE
/reports index create - -
/reports/:id show - update destroy
Sinatra式の問題点
いろいろ考えたけど• 今の 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 end
https://github.com/rails/routing_concerns
例
resources :messages, except: [ :new ] do post :trash, :restore, on: :member resources :image_attachments, only: :index end
resources :messages, except: [ :new ] do member do put :trashed delete :trashed end resources :image_attachments, only: :index end
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 fact resources :messages, except: [ :new ], partial: [:trashed] do resources :image_attachments, only: :index end
※ちょっと意味が変わる
RailsにおけるRESTfulなURL設計勉強会
Sendagaya.rb #12
7/23 (月) 19:00-
http://www.zusaar.com/event/324057