SlideShare a Scribd company logo
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 :users
end
@kenn


Is it only me who think rails routes.rb is a
mess?

         https://twitter.com/kenn/status/184076612504006656


@miyagawa


config/routes.rb goddamnit

   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
resources :users
$ rake routes
    users GET      /users(.:format)            users#index
          POST     /users(.:format)            users#create
 new_user GET      /users/new(.:format)        users#new
edit_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 over
Configuration じゃない
7アクションが「規約」なら
resources も書かなくてもいい
      のでは?
Conventional Routes


https://github.com/tkawa/conventional_routes
http://d.hatena.ne.jp/tkawa/20120407/p1
Conventional Routes
app/controllers/admin/special/licenses_controller.rb
app/controllers/admin/users_controller.rb
app/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.rb をもう一度考えてみた #shibuyarb
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
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|
  end

end
Astaire


https://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
  end

end
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
  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

More Related Content

PDF
Rails Gems realize RESTful modeling patterns
Toru Kawamura
 
KEY
BEAR v0.9 (Saturday)
Akihito Koriyama
 
PDF
Hypermedia: The Missing Element to Building Adaptable Web APIs in Rails
Toru Kawamura
 
KEY
Tricky Migrations
Stephanie Leary
 
PDF
Introduction to WordPress Theme Development
Sitdhibong Laokok
 
PDF
建立前端開發團隊 - 2011 中華電信訓練所版
Joseph Chiang
 
PPSX
WordPress Theme Design and Development Workshop - Day 2
Mizanur Rahaman Mizan
 
PDF
分享無名小站 API
Joseph Chiang
 
Rails Gems realize RESTful modeling patterns
Toru Kawamura
 
BEAR v0.9 (Saturday)
Akihito Koriyama
 
Hypermedia: The Missing Element to Building Adaptable Web APIs in Rails
Toru Kawamura
 
Tricky Migrations
Stephanie Leary
 
Introduction to WordPress Theme Development
Sitdhibong Laokok
 
建立前端開發團隊 - 2011 中華電信訓練所版
Joseph Chiang
 
WordPress Theme Design and Development Workshop - Day 2
Mizanur Rahaman Mizan
 
分享無名小站 API
Joseph Chiang
 

What's hot (19)

ZIP
Rails 3 (beta) Roundup
Wayne Carter
 
PPTX
The Way to Theme Enlightenment 2017
Amanda Giles
 
PDF
Becoming a better WordPress Developer
Joey Kudish
 
PPT
Architecture of Drupal - Drupal Camp
Dipen Chaudhary
 
KEY
Asset Pipeline
Eric Berry
 
KEY
Custom Post Types in Depth at WordCamp Montreal
Joey Kudish
 
PPTX
WordPress Theme Development
Bijay Oli
 
ODP
Drupal Theme Development - DrupalCon Chicago 2011
Ryan Price
 
PDF
Intro to WordPress theme development
Thad Allender
 
PDF
Cms & wordpress theme development 2011
Dave Wallace
 
PPTX
PyGrunn 2017 - Django Performance Unchained - slides
Artur Barseghyan
 
PDF
Grok Drupal (7) Theming - 2011 Feb update
Laura Scott
 
PDF
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Michael Pirnat
 
PDF
Word press templates
Dan Phiffer
 
PDF
Grok Drupal (7) Theming
PINGV
 
PDF
Drupal 7 Theme System
Peter Arato
 
PDF
C3 웹기술로만드는모바일앱
NAVER D2
 
PDF
Python & Django TTT
kevinvw
 
PPTX
WordPress Themes 101 - PSUWeb13 Workshop
Curtiss Grymala
 
Rails 3 (beta) Roundup
Wayne Carter
 
The Way to Theme Enlightenment 2017
Amanda Giles
 
Becoming a better WordPress Developer
Joey Kudish
 
Architecture of Drupal - Drupal Camp
Dipen Chaudhary
 
Asset Pipeline
Eric Berry
 
Custom Post Types in Depth at WordCamp Montreal
Joey Kudish
 
WordPress Theme Development
Bijay Oli
 
Drupal Theme Development - DrupalCon Chicago 2011
Ryan Price
 
Intro to WordPress theme development
Thad Allender
 
Cms & wordpress theme development 2011
Dave Wallace
 
PyGrunn 2017 - Django Performance Unchained - slides
Artur Barseghyan
 
Grok Drupal (7) Theming - 2011 Feb update
Laura Scott
 
Shiny, Let’s Be Bad Guys: Exploiting and Mitigating the Top 10 Web App Vulner...
Michael Pirnat
 
Word press templates
Dan Phiffer
 
Grok Drupal (7) Theming
PINGV
 
Drupal 7 Theme System
Peter Arato
 
C3 웹기술로만드는모바일앱
NAVER D2
 
Python & Django TTT
kevinvw
 
WordPress Themes 101 - PSUWeb13 Workshop
Curtiss Grymala
 
Ad

Similar to routes.rb をもう一度考えてみた #shibuyarb (20)

KEY
20120121 rbc rails_routing
Takeshi AKIMA
 
KEY
Rails Routing and URL design
hiq5
 
PDF
RailsスタイルからRESTを学ぼう よちがや.rb
Toru Kawamura
 
PDF
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
Masakuni Kato
 
PDF
The Rails Way
Michał Orman
 
PDF
Ruby on Rails - Introduction
Vagmi Mudumbai
 
PDF
Whats newrails4 20130406
Goh Matsumoto
 
PPT
Rest in Rails
Chamnap Chhorn
 
PDF
Rails 3 Beautiful Code
GreggPollack
 
PDF
浜松Rails3道場 其の参 Controller編
Masakuni Kato
 
PDF
Rest And Rails
Kaushik Jha
 
PDF
Les nouveautés de Rails 3
LINAGORA
 
KEY
Rails by example
Angelo van der Sijpt
 
KEY
Routing 2, Season 1
RORLAB
 
PDF
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
 
PDF
Consuming REST services with ActiveResource
Wolfram Arnold
 
PDF
Advanced Restful Rails - Europe
Ben Scofield
 
PDF
Ruby on Rails : RESTful 和 Ajax
Wen-Tien Chang
 
PDF
Ruby on Rails na Unip
Fabio Akita
 
KEY
Ruby/Rails
rstankov
 
20120121 rbc rails_routing
Takeshi AKIMA
 
Rails Routing and URL design
hiq5
 
RailsスタイルからRESTを学ぼう よちがや.rb
Toru Kawamura
 
浜松Rails3道場 其の壱 プロジェクト作成〜Rouging編
Masakuni Kato
 
The Rails Way
Michał Orman
 
Ruby on Rails - Introduction
Vagmi Mudumbai
 
Whats newrails4 20130406
Goh Matsumoto
 
Rest in Rails
Chamnap Chhorn
 
Rails 3 Beautiful Code
GreggPollack
 
浜松Rails3道場 其の参 Controller編
Masakuni Kato
 
Rest And Rails
Kaushik Jha
 
Les nouveautés de Rails 3
LINAGORA
 
Rails by example
Angelo van der Sijpt
 
Routing 2, Season 1
RORLAB
 
Ruby on Rails at PROMPT ISEL '11
Pedro Cunha
 
Consuming REST services with ActiveResource
Wolfram Arnold
 
Advanced Restful Rails - Europe
Ben Scofield
 
Ruby on Rails : RESTful 和 Ajax
Wen-Tien Chang
 
Ruby on Rails na Unip
Fabio Akita
 
Ruby/Rails
rstankov
 
Ad

More from Toru Kawamura (9)

PDF
真のREST
Toru Kawamura
 
PDF
Web Clients for Ruby and What they should be in the future
Toru Kawamura
 
PDF
RESTful #とは RailsスタイルからRESTを学ぼう
Toru Kawamura
 
PDF
Hypermedia: The Missing Element to Building Adaptable Web APIs in Rails (増補日本語版)
Toru Kawamura
 
PDF
RESTful Meetup vol.3 Introduction
Toru Kawamura
 
PDF
リソースモデリングパターンの提案 #sendagayarb
Toru Kawamura
 
PDF
返信と@ツイートの仕様変更と提案 #twtr_hack
Toru Kawamura
 
PDF
RESTとRailsスタイル
Toru Kawamura
 
PDF
OAuth Echo の Rails Gem
Toru Kawamura
 
真のREST
Toru Kawamura
 
Web Clients for Ruby and What they should be in the future
Toru Kawamura
 
RESTful #とは RailsスタイルからRESTを学ぼう
Toru Kawamura
 
Hypermedia: The Missing Element to Building Adaptable Web APIs in Rails (増補日本語版)
Toru Kawamura
 
RESTful Meetup vol.3 Introduction
Toru Kawamura
 
リソースモデリングパターンの提案 #sendagayarb
Toru Kawamura
 
返信と@ツイートの仕様変更と提案 #twtr_hack
Toru Kawamura
 
RESTとRailsスタイル
Toru Kawamura
 
OAuth Echo の Rails Gem
Toru Kawamura
 

Recently uploaded (20)

PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
PDF
REPORT: Heating appliances market in Poland 2024
SPIUG
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Doc9.....................................
SofiaCollazos
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Software Development Methodologies in 2025
KodekX
 
Unlocking the Future- AI Agents Meet Oracle Database 23ai - AIOUG Yatra 2025.pdf
Sandesh Rao
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
Tea4chat - another LLM Project by Kerem Atam
a0m0rajab1
 
REPORT: Heating appliances market in Poland 2024
SPIUG
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
Cloud-Migration-Best-Practices-A-Practical-Guide-to-AWS-Azure-and-Google-Clou...
Artjoker Software Development Company
 

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