OSDN Git Service

Regular updates
[twpd/master.git] / rails-routes.md
1 ---
2 title: Routes
3 category: Rails
4 layout: 2017/sheet
5 ---
6
7 ## Resources
8
9 ### Multiple resources (`resources`)
10
11     resources :photos
12
13     # PhotosController:
14     # index  =>    GET /photos
15     # new    =>    GET /photos/new
16     # create =>   POST /photos/new
17     # show   =>    GET /photos/:id
18     # edit   =>    GET /photos/:id/edit
19     # update =>    PUT /photos/:id
20     # delete => DELETE /photos/:id
21     #
22     # Helpers:
23     # new_photo_path
24     # photo_path(id)
25     # edit_photo_path(id)
26
27 ### Custom actions
28
29     resources :photos do
30       member { get 'preview' }       # /photo/1/preview
31       collection { get 'search' }    # /photos/search
32
33       get 'preview', on: :member     # (..same as the first)
34     end
35
36 ### Options
37
38     resources :photos,
39       path_names: { new: 'brand_new' }    # /photos/1/brand_new
40       path: 'postings'                    # /postings
41       only: :index
42       only: [:index, :show]
43       except: :show
44       except: [:index, :show]
45
46       shallow: true                       # also generate shallow routes
47       shalow_path: 'secret'
48       shallow_prefix: 'secret'
49
50 ## Single resource (`resource`)
51
52     resource :coder
53
54     # CodersController:
55     # new    =>    GET /coder/new
56     # create =>   POST /coder/new
57     # show   =>    GET /coder
58     # edit   =>    GET /coder/edit
59     # update =>    PUT /coder
60     # delete => DELETE /coder
61
62 ## Matching (`match`)
63
64     match 'photo/:id' => 'photos#show'  # /photo/what-is-it
65     match 'photo/:id', id: /[0-9]+/     # /photo/0192
66     match 'photo/:id' => 'photos#show', constraints: { id: /[0-9]+/ }
67     match 'photo/:id', via: :get
68     match 'photo/:id', via: [:get, :post]
69
70     match 'photo/*path' => 'photos#unknown'    # /photo/what/ever
71
72     # params[:format] == 'jpg'
73     match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
74
75 ### Get/post
76
77 `get` is the same as `match via: :get`.
78
79     get 'photo/:id' => 'photos#show'
80     # same as match 'photo/:id' => 'photos#show', via: :get
81
82     post 'photo/:id' => 'photos#update'
83     # same as match 'photo/:id' => 'photos#show', via: :post
84
85 ### Redirection
86
87     match '/stories' => redirect('/posts')
88     match '/stories/:name' => redirect('/posts/%{name}')
89
90 ### Named
91
92     # logout_path
93     match 'exit' => 'sessions#destroy', as: :logout
94     
95 ### Constraints
96
97     match '/', constraints: { subdomain: 'admin' }
98
99     # admin.site.com/admin/photos
100     namespace 'admin' do
101       constraints subdomain: 'admin' do
102         resources :photos
103       end
104     end
105
106 ### Custom constraints
107
108     class BlacklistConstraint
109       def initialize
110         @ips = Blacklist.retrieve_ips
111       end
112      
113       def matches?(request)
114         @ips.include?(request.remote_ip)
115       end
116     end
117      
118     TwitterClone::Application.routes.draw do
119       match "*path" => "blacklist#index",
120         :constraints => BlacklistConstraint.new
121     end
122
123 ### Scopes
124
125     scope 'admin', constraints: { subdomain: 'admin' } do
126       resources ...
127     end
128
129 ### Rack middleware
130
131     # Yes, Sprockets is middleware
132     match '/application.js' => Sprockets
133
134 ### Route helpers
135
136     projects_path   # /projects
137     projects_url    # http://site.com/projects
138
139
140 ### Default help text
141
142     # The priority is based upon order of creation:
143     # first created -> highest priority.
144
145     # Sample of regular route:
146     match 'products/:id' => 'catalog#view'
147
148     # Keep in mind you can assign values other than :controller and :action
149
150     # Sample of named route:
151     match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase
152
153     # This route can be invoked with purchase_url(:id => product.id)
154
155     # Sample resource route (maps HTTP verbs to controller actions automatically):
156     resources :products
157
158     # Sample resource route with options:
159     resources :products do
160       member do
161         get 'short'
162         post 'toggle'
163       end
164     
165       collection do
166         get 'sold'
167       end
168     end
169
170     # Sample resource route with sub-resources:
171     resources :products do
172       resources :comments, :sales
173       resource :seller
174     end
175
176     # Sample resource route with more complex sub-resources
177     resources :products do
178       resources :comments
179       resources :sales do
180         get 'recent', :on => :collection
181       end
182     end
183
184     # Sample resource route within a namespace:
185     namespace :admin do
186       # Directs /admin/products/* to Admin::ProductsController
187       # (app/controllers/admin/products_controller.rb)
188       resources :products
189     end
190
191     # You can have the root of your site routed with "root"
192     # just remember to delete public/index.html.
193     root :to => 'welcome#index'
194
195     # See how all your routes lay out with "rake routes"
196
197     # This is a legacy wild controller route that's not recommended for RESTful applications.
198     # Note: This route will make all actions in every controller accessible via GET requests.
199     match ':controller(/:action(/:id(.:format)))'
200
201 ### References
202
203  * [Guides/Routing](http://guides.rubyonrails.org/routing.html)
204
205  * [ActionDispatch::Routing::Mapper](http://api.rubyonrails.org/classes/ActionDispatch/Routing/Mapper.html)
206     (See included modules)
207