SlideShare a Scribd company logo
1 of 18
Download to read offline
RailsによるURLの描き方
  fukajun@sendagaya.rb
誰?

○ Jun Fukaya
○ twitter : fukajun
○ minami.rb 出身

○ sendagaya.rb - 毎週 月曜日
    コンセプト : 毎週の勉強会を楽しみにすることで
         毎日の仕事やプライベートを楽しくする


○ RubyOnRails, BorneShell, Javascript, C#, PostgreSQL,
   MongoDB
今日は何をやるの?



RailsでのURLの定義する方法

          ↓

   config/routes.rb
config/routes.rbの役割

・URLを認識させる ♥

・URLとControllerのActionを結びつける
  /books/57648 → books#show

・パラメータの定義
  /maps/:latitude,:longitude 
URLとは?
ネットワーク上のリソースを一意に指し示すもの



リソースとは?
 ・レコード一件
 ・インスタンス
 ・ユーザーのアカウント1件




  Railsが取り扱うデータを一意に指し表すもの
routes.rbでURLを作成する




          URL( = リソース) に対して
       HTTPメソッド( = 行う操作)を決める

 行う操作:どのcontrollerのaction紐付けるかを決める。
HTTPメソッドの役割(get, post)
リソースの取得, 参照
 GET /users                                   ミルミルー
  get :users, :to => "users#index"
                                                    1       2       3
                                     users/


リソースの作成, 追加
 POST /users                                            ?


  post :users, :to => "users#create"
                                                                    追加


                                                1       2       3
                                     users/
HTTPメソッドの役割(put, delete)
                                  身長: 171cm, 体重: 60kg
リソースの変更                                                          更新
                                             1   2   3   4

 PUT /users/:id                   users/


  put 'users/:id', :to => 'users#update'


リソースの削除
 DELETE /users/:id
  delete 'users/:id', :to => 'users#destroy'
                                                             削除


                                                 1   2   3   4
                                    users/
CRUDなURLを一括で定義
(resources)
GET /users       users#index
GET /users/new users#new
POST /users       users#create
GET /users/:id    users#show
GET /users/:id/edit edit
PUT /users/:id update
DELETE /users/:id destroy


 resources :users
resourcesにURLを追加したい
GET /users/friend
 resources :users do
   get :friend, :on => :collection
 end

GET /entries/:id/preview
 resources :users do
   get :preview, :on => :member
 end
resourcesで定義したURLを限定する
GET /users       users#index
GET /users/new users#new       # 不要
POST /users       users#create
GET /users/:id    users#show
GET /users/:id/edit edit    # 不要
PUT /users/:id update
DELETE /users/:id destroy

resources :users, :only => [:index, :create, :update, :
destroy]
OR
resources :users, :except => [:new, :edit]
1つのリソースに対して
CRUDなURLを一括で定義(resource)
GET /profile/new        new
POST /profile           create
GET /profile/          show
GET /profile/edit       edit
PUT /profile           update
DELETE /profile         destroy

 resource :profile, :controller => :users
リソースが持っている持っている
リソースへのURLを定義したい
GET /posts/:post_id/comments
POST /posts/:post_id/comments
GET /posts/:post_id/comments/new
GET /posts/:post_id/comments/:id/edit
GET /posts/:post_id/comments/:id
PUT /posts/:post_id/comments/:id
DELETE /posts/:post_id/comments/:id

resources :posts do
 resources :comments
end
URLにprefixを付けたい場合
URL : /admin/products

namespace :admin do
 resource :setting, :except => [:delete]
end

app/controllers/admin/products_controller
URLにprefixを付けたい場合
URL : /api/products

scope '/api' do
 resource :setting, :except => [:delete]
end

app/controllers/products_controller
       ↑
      api/ディレクトリが無くても良い
まとめ

● get, post, put, deleteの役割を意識する

● resources, resourceを使うと便利

● 従属関係にあるものは、resourcesネスト

● prefixをつけるときは、namespace, scope
忘れてた

GET /

root :to => "backet#index"
参考資料
● Rails3のroutesまとめ
http://irohiroki.com/2010/08/29/rails3-routes

More Related Content

Similar to Railsによるurlの描き方

deviseを利用した認証について@Minamirb
deviseを利用した認証について@Minamirbdeviseを利用した認証について@Minamirb
deviseを利用した認証について@MinamirbJun Fukaya
 
はじめてのCouch db
はじめてのCouch dbはじめてのCouch db
はじめてのCouch dbEiji Kuroda
 
Rails基礎講座 part.2
Rails基礎講座 part.2Rails基礎講座 part.2
Rails基礎講座 part.2Jun Yokoyama
 
Web技術勉強会23回目
Web技術勉強会23回目Web技術勉強会23回目
Web技術勉強会23回目龍一 田中
 
named_scope more detail - WebCareer
named_scope more detail - WebCareernamed_scope more detail - WebCareer
named_scope more detail - WebCareerKyosuke MOROHASHI
 
Ruby on Rails Tutorial
Ruby on Rails TutorialRuby on Rails Tutorial
Ruby on Rails TutorialKen Iiboshi
 
Ruby on Rails Tutorial Chapter8-10
Ruby on Rails Tutorial Chapter8-10Ruby on Rails Tutorial Chapter8-10
Ruby on Rails Tutorial Chapter8-10Sea Mountain
 
RESTとRailsスタイル
RESTとRailsスタイルRESTとRailsスタイル
RESTとRailsスタイルToru Kawamura
 
Okinawa.rb 第2回勉強会
Okinawa.rb 第2回勉強会Okinawa.rb 第2回勉強会
Okinawa.rb 第2回勉強会Naoki Takaesu
 
Garageを触ってみた
Garageを触ってみたGarageを触ってみた
Garageを触ってみたYoichi Toyota
 

Similar to Railsによるurlの描き方 (11)

deviseを利用した認証について@Minamirb
deviseを利用した認証について@Minamirbdeviseを利用した認証について@Minamirb
deviseを利用した認証について@Minamirb
 
はじめてのCouch db
はじめてのCouch dbはじめてのCouch db
はじめてのCouch db
 
Rails基礎講座 part.2
Rails基礎講座 part.2Rails基礎講座 part.2
Rails基礎講座 part.2
 
Web技術勉強会23回目
Web技術勉強会23回目Web技術勉強会23回目
Web技術勉強会23回目
 
named_scope more detail - WebCareer
named_scope more detail - WebCareernamed_scope more detail - WebCareer
named_scope more detail - WebCareer
 
Ruby on Rails Tutorial
Ruby on Rails TutorialRuby on Rails Tutorial
Ruby on Rails Tutorial
 
Ruby on Rails Tutorial Chapter8-10
Ruby on Rails Tutorial Chapter8-10Ruby on Rails Tutorial Chapter8-10
Ruby on Rails Tutorial Chapter8-10
 
WordPress と Bootstrap
WordPress と BootstrapWordPress と Bootstrap
WordPress と Bootstrap
 
RESTとRailsスタイル
RESTとRailsスタイルRESTとRailsスタイル
RESTとRailsスタイル
 
Okinawa.rb 第2回勉強会
Okinawa.rb 第2回勉強会Okinawa.rb 第2回勉強会
Okinawa.rb 第2回勉強会
 
Garageを触ってみた
Garageを触ってみたGarageを触ってみた
Garageを触ってみた
 

Recently uploaded

Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Danieldanielhu54
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムsugiuralab
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...Toru Tamaki
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdftaisei2219
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNetToru Tamaki
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略Ryo Sasaki
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A surveyToru Tamaki
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Yuma Ohgami
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものですiPride Co., Ltd.
 

Recently uploaded (9)

Postman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By DanielPostman LT Fukuoka_Quick Prototype_By Daniel
Postman LT Fukuoka_Quick Prototype_By Daniel
 
スマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システムスマートフォンを用いた新生児あやし動作の教示システム
スマートフォンを用いた新生児あやし動作の教示システム
 
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
論文紹介:Content-Aware Token Sharing for Efficient Semantic Segmentation With Vis...
 
TSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdfTSAL operation mechanism and circuit diagram.pdf
TSAL operation mechanism and circuit diagram.pdf
 
論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet論文紹介:Automated Classification of Model Errors on ImageNet
論文紹介:Automated Classification of Model Errors on ImageNet
 
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
[DevOpsDays Tokyo 2024] 〜デジタルとアナログのはざまに〜 スマートビルディング爆速開発を支える 自動化テスト戦略
 
論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey論文紹介:Semantic segmentation using Vision Transformers: A survey
論文紹介:Semantic segmentation using Vision Transformers: A survey
 
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
Open Source UN-Conference 2024 Kawagoe - 独自OS「DaisyOS GB」の紹介
 
SOPを理解する 2024/04/19 の勉強会で発表されたものです
SOPを理解する       2024/04/19 の勉強会で発表されたものですSOPを理解する       2024/04/19 の勉強会で発表されたものです
SOPを理解する 2024/04/19 の勉強会で発表されたものです
 

Railsによるurlの描き方