OSDN Git Service

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