Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 261 lines (257 sloc) 8.345 kB
c42a7a0 @kennyj Support unicode character route in config/routes.rb.
kennyj authored
1 # encoding: UTF-8
b540eca @jeremy Consolidate Object#to_param and #to_query core extensions
jeremy authored
2 require 'active_support/core_ext/object/to_param'
a1df259 @josh Replace decaying routing internals w/ rack-mount
josh authored
3 require 'active_support/core_ext/regexp'
b20c575 @jamis New routes implementation. Simpler, faster, easier to understand. The…
jamis authored
4
a74022e @josh Move Routing into AD
josh authored
5 module ActionDispatch
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
6 # The routing module provides URL rewriting in native Ruby. It's a way to
7 # redirect incoming requests to controllers and actions. This replaces
5ead15b @tilsammans Rework the routing documentation.
tilsammans authored
8 # mod_rewrite rules. Best of all, Rails' \Routing works with any web server.
dc4eec1 @lifo Merge docrails:
lifo authored
9 # Routes are defined in <tt>config/routes.rb</tt>.
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
10 #
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
11 # Think of creating routes as drawing a map for your requests. The map tells
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
12 # them where to go based on some predefined pattern:
13 #
1a35b62 @stevenh512 Removed deprecated |map| block argument from routing docs since it is…
stevenh512 authored
14 # AppName::Application.routes.draw do
64092de @fxn Improve documentation coverage and markup
fxn authored
15 # Pattern 1 tells some request to go to one place
16 # Pattern 2 tell them to go to another
17 # ...
18 # end
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
19 #
20 # The following symbols are special:
21 #
22 # :controller maps to your controller name
23 # :action maps to an action with your controllers
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
24 #
aa4af60 @lifo Improve documentation.
lifo authored
25 # Other names simply map to a parameter as in the case of <tt>:id</tt>.
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
26 #
5ead15b @tilsammans Rework the routing documentation.
tilsammans authored
27 # == Resources
28 #
29 # Resource routing allows you to quickly declare all of the common routes
30 # for a given resourceful controller. Instead of declaring separate routes
31 # for your +index+, +show+, +new+, +edit+, +create+, +update+ and +destroy+
32 # actions, a resourceful route declares them in a single line of code:
33 #
34 # resources :photos
35 #
36 # Sometimes, you have a resource that clients always look up without
37 # referencing an ID. A common example, /profile always shows the profile of
38 # the currently logged in user. In this case, you can use a singular resource
39 # to map /profile (rather than /profile/:id) to the show action.
40 #
41 # resource :profile
42 #
43 # It's common to have resources that are logically children of other
44 # resources:
45 #
46 # resources :magazines do
47 # resources :ads
48 # end
49 #
50 # You may wish to organize groups of controllers under a namespace. Most
51 # commonly, you might group a number of administrative controllers under
52 # an +admin+ namespace. You would place these controllers under the
56a7db6 @smartinez87 added some fixed fonts
smartinez87 authored
53 # <tt>app/controllers/admin</tt> directory, and you can group them together
54 # in your router:
5ead15b @tilsammans Rework the routing documentation.
tilsammans authored
55 #
56 # namespace "admin" do
57 # resources :posts, :comments
58 # end
59 #
38d728f add cross-references and documentation for scope
Jonathan Dance + Gabriel Horner authored
60 # Alternately, you can add prefixes to your path without using a separate
61 # directory by using +scope+. +scope+ takes additional options which
62 # apply to all enclosed routes.
63 #
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
64 # scope path: "/cpanel", as: 'admin' do
38d728f add cross-references and documentation for scope
Jonathan Dance + Gabriel Horner authored
65 # resources :posts, :comments
66 # end
67 #
68 # For more, see <tt>Routing::Mapper::Resources#resources</tt>,
69 # <tt>Routing::Mapper::Scoping#namespace</tt>, and
70 # <tt>Routing::Mapper::Scoping#scope</tt>.
71 #
7305ef8 @trevorturk The HTTP method helpers are preferred over match and should be explai…
trevorturk authored
72 # == Non-resourceful routes
73 #
74 # For routes that don't fit the <tt>resources</tt> mold, you can use the HTTP helper
75 # methods <tt>get</tt>, <tt>post</tt>, <tt>patch</tt>, <tt>put</tt> and <tt>delete</tt>.
76 #
77 # get 'post/:id' => 'posts#show'
78 # post 'post/:id' => 'posts#create_comment'
79 #
80 # If your route needs to respond to more than one HTTP method (or all methods) then using the
81 # <tt>:via</tt> option on <tt>match</tt> is preferable.
82 #
83 # match 'post/:id' => 'posts#show', via: [:get, :post]
84 #
85 # Now, if you POST to <tt>/posts/:id</tt>, it will route to the <tt>create_comment</tt> action. A GET on the same
86 # URL will route to the <tt>show</tt> action.
87 #
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
88 # == Named routes
89 #
df8852d @rubys Make rdoc match current API
rubys authored
90 # Routes can be named by passing an <tt>:as</tt> option,
c5f7997 @jeremy Improve routes documentation. Closes #7095.
jeremy authored
91 # allowing for easy reference within your source as +name_of_route_url+
92 # for the full URL and +name_of_route_path+ for the URI path.
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
93 #
94 # Example:
64092de @fxn Improve documentation coverage and markup
fxn authored
95 #
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
96 # # In routes.rb
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
97 # get '/login' => 'accounts#login', as: 'login'
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
98 #
99 # # With render, redirect_to, tests, etc.
100 # redirect_to login_url
101 #
102 # Arguments can be passed as well.
103 #
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
104 # redirect_to show_item_path(id: 25)
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
105 #
dc364fd @rohit API Docs: Fixes to the Routing docs
rohit authored
106 # Use <tt>root</tt> as a shorthand to name a route for the root path "/".
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
107 #
c5f7997 @jeremy Improve routes documentation. Closes #7095.
jeremy authored
108 # # In routes.rb
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
109 # root to: 'blogs#index'
c5f7997 @jeremy Improve routes documentation. Closes #7095.
jeremy authored
110 #
111 # # would recognize http://www.example.com/ as
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
112 # params = { controller: 'blogs', action: 'index' }
c5f7997 @jeremy Improve routes documentation. Closes #7095.
jeremy authored
113 #
114 # # and provide these named routes
115 # root_url # => 'http://www.example.com/'
dc364fd @rohit API Docs: Fixes to the Routing docs
rohit authored
116 # root_path # => '/'
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
117 #
df8852d @rubys Make rdoc match current API
rubys authored
118 # Note: when using +controller+, the route is simply named after the
c5f7997 @jeremy Improve routes documentation. Closes #7095.
jeremy authored
119 # method you call on the block parameter rather than map.
120 #
121 # # In routes.rb
df8852d @rubys Make rdoc match current API
rubys authored
122 # controller :blog do
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
123 # get 'blog/show' => :list
124 # get 'blog/delete' => :delete
125 # get 'blog/edit/:id' => :edit
c5f7997 @jeremy Improve routes documentation. Closes #7095.
jeremy authored
126 # end
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
127 #
c5f7997 @jeremy Improve routes documentation. Closes #7095.
jeremy authored
128 # # provides named routes for show, delete, and edit
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
129 # link_to @article.title, show_path(id: @article.id)
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
130 #
c5f7997 @jeremy Improve routes documentation. Closes #7095.
jeremy authored
131 # == Pretty URLs
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
132 #
133 # Routes can generate pretty URLs. For example:
134 #
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
135 # get '/articles/:year/:month/:day' => 'articles#find_by_id', constraints: {
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
136 # year: /\d{4}/,
137 # month: /\d{1,2}/,
138 # day: /\d{1,2}/
df8852d @rubys Make rdoc match current API
rubys authored
139 # }
64092de @fxn Improve documentation coverage and markup
fxn authored
140 #
141 # Using the route above, the URL "http://localhost:3000/articles/2005/11/06"
142 # maps to
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
143 #
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
144 # params = {year: '2005', month: '11', day: '06'}
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
145 #
146 # == Regular Expressions and parameters
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
147 # You can specify a regular expression to define a format for a parameter.
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
148 #
df8852d @rubys Make rdoc match current API
rubys authored
149 # controller 'geocode' do
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
150 # get 'geocode/:postalcode' => :show, constraints: {
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
151 # postalcode: /\d{5}(-\d{4})?/
df8852d @rubys Make rdoc match current API
rubys authored
152 # }
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
153 #
df8852d @rubys Make rdoc match current API
rubys authored
154 # Constraints can include the 'ignorecase' and 'extended syntax' regular
db3a60e @dhh Added support for regexp flags like ignoring case in the :requirement…
dhh authored
155 # expression modifiers:
156 #
df8852d @rubys Make rdoc match current API
rubys authored
157 # controller 'geocode' do
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
158 # get 'geocode/:postalcode' => :show, constraints: {
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
159 # postalcode: /hx\d\d\s\d[a-z]{2}/i
df8852d @rubys Make rdoc match current API
rubys authored
160 # }
161 # end
db3a60e @dhh Added support for regexp flags like ignoring case in the :requirement…
dhh authored
162 #
df8852d @rubys Make rdoc match current API
rubys authored
163 # controller 'geocode' do
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
164 # get 'geocode/:postalcode' => :show, constraints: {
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
165 # postalcode: /# Postcode format
df8852d @rubys Make rdoc match current API
rubys authored
166 # \d{5} #Prefix
167 # (-\d{4})? #Suffix
168 # /x
169 # }
170 # end
db3a60e @dhh Added support for regexp flags like ignoring case in the :requirement…
dhh authored
171 #
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
172 # Using the multiline modifier will raise an +ArgumentError+.
db3a60e @dhh Added support for regexp flags like ignoring case in the :requirement…
dhh authored
173 # Encoding regular expression modifiers are silently ignored. The
174 # match will always use the default encoding or ASCII.
175 #
5ead15b @tilsammans Rework the routing documentation.
tilsammans authored
176 # == External redirects
177 #
178 # You can redirect any path to another path using the redirect helper in your router:
179 #
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
180 # get "/stories" => redirect("/posts")
5ead15b @tilsammans Rework the routing documentation.
tilsammans authored
181 #
c42a7a0 @kennyj Support unicode character route in config/routes.rb.
kennyj authored
182 # == Unicode character routes
183 #
184 # You can specify unicode character routes in your router:
185 #
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
186 # get "こんにちは" => "welcome#index"
c42a7a0 @kennyj Support unicode character route in config/routes.rb.
kennyj authored
187 #
5ead15b @tilsammans Rework the routing documentation.
tilsammans authored
188 # == Routing to Rack Applications
189 #
190 # Instead of a String, like <tt>posts#index</tt>, which corresponds to the
191 # index action in the PostsController, you can specify any Rack application
192 # as the endpoint for a matcher:
193 #
efea57a @trevorturk Using match without via is deprecated
trevorturk authored
194 # get "/application.js" => Sprockets
5ead15b @tilsammans Rework the routing documentation.
tilsammans authored
195 #
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
196 # == Reloading routes
197 #
198 # You can reload routes if you feel you must:
199 #
53b34e8 @josevalim Avoid calls to Rails::Application since this is not the official API.
josevalim authored
200 # Rails.application.reload_routes!
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
201 #
bd03bf9 @NZKoz Make sure that custom inflections are picked up by map.resources by t…
NZKoz authored
202 # This will clear all named routes and reload routes.rb if the file has been modified from
dc4eec1 @lifo Merge docrails:
lifo authored
203 # last load. To absolutely force reloading, use <tt>reload!</tt>.
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
204 #
205 # == Testing Routes
206 #
207 # The two main methods for testing your routes:
208 #
209 # === +assert_routing+
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
210 #
64092de @fxn Improve documentation coverage and markup
fxn authored
211 # def test_movie_route_properly_splits
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
212 # opts = {controller: "plugin", action: "checkout", id: "2"}
64092de @fxn Improve documentation coverage and markup
fxn authored
213 # assert_routing "plugin/checkout/2", opts
214 # end
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
215 #
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
216 # +assert_routing+ lets you test whether or not the route properly resolves into options.
217 #
218 # === +assert_recognizes+
219 #
64092de @fxn Improve documentation coverage and markup
fxn authored
220 # def test_route_has_options
d20a529 @AvnerCohen 1.9 hash syntax changes to docs
AvnerCohen authored
221 # opts = {controller: "plugin", action: "show", id: "12"}
64092de @fxn Improve documentation coverage and markup
fxn authored
222 # assert_recognizes opts, "/plugins/show/12"
223 # end
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
224 #
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
225 # Note the subtle difference between the two: +assert_routing+ tests that
7464a39 @NZKoz Minor documentation enhancements and white-space fixes. Closes #9819 …
NZKoz authored
226 # a URL fits options while +assert_recognizes+ tests that a URL
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
227 # breaks into parameters properly.
228 #
229 # In tests you can simply pass the URL or named route to +get+ or +post+.
230 #
64092de @fxn Improve documentation coverage and markup
fxn authored
231 # def send_to_jail
232 # get '/jail'
233 # assert_response :success
234 # assert_template "jail/front"
235 # end
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
236 #
64092de @fxn Improve documentation coverage and markup
fxn authored
237 # def goes_to_login
238 # get login_url
239 # #...
240 # end
40c86a7 @dhh Documentation for routes (closes #5165) [rramdas@gmail.com]
dhh authored
241 #
0c6150a Make the routes rake task more discoverable but documenting it in the…
Marcel Molina authored
242 # == View a list of all your routes
243 #
5ead15b @tilsammans Rework the routing documentation.
tilsammans authored
244 # rake routes
245 #
246 # Target specific controllers by prefixing the command with <tt>CONTROLLER=x</tt>.
0c6150a Make the routes rake task more discoverable but documenting it in the…
Marcel Molina authored
247 #
b20c575 @jamis New routes implementation. Simpler, faster, easier to understand. The…
jamis authored
248 module Routing
e4fd226 @gsamokovarov Dry-up ActionDispatch::Routing autoloads
gsamokovarov authored
249 extend ActiveSupport::Autoload
250
251 autoload :Mapper
252 autoload :RouteSet
253 autoload :RoutesProxy
254 autoload :UrlFor
255 autoload :PolymorphicRoutes
a74022e @josh Move Routing into AD
josh authored
256
c10bf82 @pixeltrix Remove routing implementation details from RDoc
pixeltrix authored
257 SEPARATORS = %w( / . ? ) #:nodoc:
002713c @dlee Add config.default_method_for_update to support PATCH
dlee authored
258 HTTP_METHODS = [:get, :head, :post, :patch, :put, :delete, :options] #:nodoc:
b1999be @dhh A hopefully more successful attempt at the Routing branch merge
dhh authored
259 end
148202d @jeremy Fixed optimized route segment escaping. Closes #9562.
jeremy authored
260 end
Something went wrong with that request. Please try again.