routing.rb 8.0 KB
Newer Older
1
# encoding: UTF-8
2

J
Joshua Peek 已提交
3
module ActionDispatch
4 5
  # The routing module provides URL rewriting in native Ruby. It's a way to
  # redirect incoming requests to controllers and actions. This replaces
J
Joost Baaij 已提交
6
  # mod_rewrite rules. Best of all, Rails' \Routing works with any web server.
P
Pratik Naik 已提交
7
  # Routes are defined in <tt>config/routes.rb</tt>.
8
  #
9
  # Think of creating routes as drawing a map for your requests. The map tells
10 11
  # them where to go based on some predefined pattern:
  #
12
  #   Rails.application.routes.draw do
13 14 15 16
  #     Pattern 1 tells some request to go to one place
  #     Pattern 2 tell them to go to another
  #     ...
  #   end
17 18 19 20 21
  #
  # The following symbols are special:
  #
  #   :controller maps to your controller name
  #   :action     maps to an action with your controllers
22
  #
P
Pratik Naik 已提交
23
  # Other names simply map to a parameter as in the case of <tt>:id</tt>.
24
  #
J
Joost Baaij 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  # == Resources
  #
  # Resource routing allows you to quickly declare all of the common routes
  # for a given resourceful controller. Instead of declaring separate routes
  # for your +index+, +show+, +new+, +edit+, +create+, +update+ and +destroy+
  # actions, a resourceful route declares them in a single line of code:
  #
  #  resources :photos
  #
  # Sometimes, you have a resource that clients always look up without
  # referencing an ID. A common example, /profile always shows the profile of
  # the currently logged in user. In this case, you can use a singular resource
  # to map /profile (rather than /profile/:id) to the show action.
  #
  #  resource :profile
  #
  # It's common to have resources that are logically children of other
  # resources:
  #
  #   resources :magazines do
  #     resources :ads
  #   end
  #
  # You may wish to organize groups of controllers under a namespace. Most
  # commonly, you might group a number of administrative controllers under
  # an +admin+ namespace. You would place these controllers under the
S
Sebastian Martinez 已提交
51 52
  # <tt>app/controllers/admin</tt> directory, and you can group them together
  # in your router:
J
Joost Baaij 已提交
53 54 55 56 57
  #
  #   namespace "admin" do
  #     resources :posts, :comments
  #   end
  #
58 59 60 61
  # Alternately, you can add prefixes to your path without using a separate
  # directory by using +scope+. +scope+ takes additional options which
  # apply to all enclosed routes.
  #
A
AvnerCohen 已提交
62
  #   scope path: "/cpanel", as: 'admin' do
63 64 65 66 67 68 69
  #     resources :posts, :comments
  #   end
  #
  # For more, see <tt>Routing::Mapper::Resources#resources</tt>,
  # <tt>Routing::Mapper::Scoping#namespace</tt>, and
  # <tt>Routing::Mapper::Scoping#scope</tt>.
  #
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  # == Non-resourceful routes
  #
  # For routes that don't fit the <tt>resources</tt> mold, you can use the HTTP helper
  # methods <tt>get</tt>, <tt>post</tt>, <tt>patch</tt>, <tt>put</tt> and <tt>delete</tt>.
  #
  #   get 'post/:id' => 'posts#show'
  #   post 'post/:id' => 'posts#create_comment'
  #
  # If your route needs to respond to more than one HTTP method (or all methods) then using the
  # <tt>:via</tt> option on <tt>match</tt> is preferable.
  #
  #   match 'post/:id' => 'posts#show', via: [:get, :post]
  #
  # Now, if you POST to <tt>/posts/:id</tt>, it will route to the <tt>create_comment</tt> action. A GET on the same
  # URL will route to the <tt>show</tt> action.
  #
86 87
  # == Named routes
  #
S
Sam Ruby 已提交
88
  # Routes can be named by passing an <tt>:as</tt> option,
89 90
  # allowing for easy reference within your source as +name_of_route_url+
  # for the full URL and +name_of_route_path+ for the URI path.
91 92
  #
  # Example:
93
  #
94
  #   # In routes.rb
95
  #   get '/login' => 'accounts#login', as: 'login'
96 97 98 99 100 101
  #
  #   # With render, redirect_to, tests, etc.
  #   redirect_to login_url
  #
  # Arguments can be passed as well.
  #
A
AvnerCohen 已提交
102
  #   redirect_to show_item_path(id: 25)
103
  #
104
  # Use <tt>root</tt> as a shorthand to name a route for the root path "/".
105
  #
106
  #   # In routes.rb
A
AvnerCohen 已提交
107
  #   root to: 'blogs#index'
108 109
  #
  #   # would recognize http://www.example.com/ as
A
AvnerCohen 已提交
110
  #   params = { controller: 'blogs', action: 'index' }
111 112 113
  #
  #   # and provide these named routes
  #   root_url   # => 'http://www.example.com/'
114
  #   root_path  # => '/'
115
  #
S
Sam Ruby 已提交
116
  # Note: when using +controller+, the route is simply named after the
117 118 119
  # method you call on the block parameter rather than map.
  #
  #   # In routes.rb
S
Sam Ruby 已提交
120
  #   controller :blog do
121 122 123
  #     get 'blog/show'     => :list
  #     get 'blog/delete'   => :delete
  #     get 'blog/edit/:id' => :edit
124
  #   end
125
  #
126
  #   # provides named routes for show, delete, and edit
A
AvnerCohen 已提交
127
  #   link_to @article.title, show_path(id: @article.id)
128
  #
129
  # == Pretty URLs
130 131 132
  #
  # Routes can generate pretty URLs. For example:
  #
133
  #   get '/articles/:year/:month/:day' => 'articles#find_by_id', constraints: {
A
AvnerCohen 已提交
134 135 136
  #     year:       /\d{4}/,
  #     month:      /\d{1,2}/,
  #     day:        /\d{1,2}/
S
Sam Ruby 已提交
137
  #   }
138 139 140
  #
  # Using the route above, the URL "http://localhost:3000/articles/2005/11/06"
  # maps to
141
  #
A
AvnerCohen 已提交
142
  #   params = {year: '2005', month: '11', day: '06'}
143 144
  #
  # == Regular Expressions and parameters
145
  # You can specify a regular expression to define a format for a parameter.
146
  #
S
Sam Ruby 已提交
147
  #   controller 'geocode' do
148
  #     get 'geocode/:postalcode' => :show, constraints: {
A
AvnerCohen 已提交
149
  #       postalcode: /\d{5}(-\d{4})?/
S
Sam Ruby 已提交
150
  #     }
151
  #
S
Sam Ruby 已提交
152
  # Constraints can include the 'ignorecase' and 'extended syntax' regular
153 154
  # expression modifiers:
  #
S
Sam Ruby 已提交
155
  #   controller 'geocode' do
156
  #     get 'geocode/:postalcode' => :show, constraints: {
A
AvnerCohen 已提交
157
  #       postalcode: /hx\d\d\s\d[a-z]{2}/i
S
Sam Ruby 已提交
158 159
  #     }
  #   end
160
  #
S
Sam Ruby 已提交
161
  #   controller 'geocode' do
162
  #     get 'geocode/:postalcode' => :show, constraints: {
A
AvnerCohen 已提交
163
  #       postalcode: /# Postcode format
S
Sam Ruby 已提交
164 165 166 167 168
  #          \d{5} #Prefix
  #          (-\d{4})? #Suffix
  #          /x
  #     }
  #   end
169
  #
170
  # Using the multiline modifier will raise an +ArgumentError+.
171 172 173
  # Encoding regular expression modifiers are silently ignored. The
  # match will always use the default encoding or ASCII.
  #
J
Joost Baaij 已提交
174 175 176 177
  # == External redirects
  #
  # You can redirect any path to another path using the redirect helper in your router:
  #
178
  #   get "/stories" => redirect("/posts")
J
Joost Baaij 已提交
179
  #
180 181 182 183
  # == Unicode character routes
  #
  # You can specify unicode character routes in your router:
  #
184
  #   get "こんにちは" => "welcome#index"
185
  #
J
Joost Baaij 已提交
186 187 188 189 190 191
  # == Routing to Rack Applications
  #
  # Instead of a String, like <tt>posts#index</tt>, which corresponds to the
  # index action in the PostsController, you can specify any Rack application
  # as the endpoint for a matcher:
  #
192
  #   get "/application.js" => Sprockets
J
Joost Baaij 已提交
193
  #
194 195 196 197
  # == Reloading routes
  #
  # You can reload routes if you feel you must:
  #
198
  #   Rails.application.reload_routes!
199
  #
200
  # This will clear all named routes and reload routes.rb if the file has been modified from
P
Pratik Naik 已提交
201
  # last load. To absolutely force reloading, use <tt>reload!</tt>.
202 203 204 205 206 207
  #
  # == Testing Routes
  #
  # The two main methods for testing your routes:
  #
  # === +assert_routing+
208
  #
209
  #   def test_movie_route_properly_splits
A
AvnerCohen 已提交
210
  #    opts = {controller: "plugin", action: "checkout", id: "2"}
211 212
  #    assert_routing "plugin/checkout/2", opts
  #   end
213
  #
214 215 216 217
  # +assert_routing+ lets you test whether or not the route properly resolves into options.
  #
  # === +assert_recognizes+
  #
218
  #   def test_route_has_options
A
AvnerCohen 已提交
219
  #    opts = {controller: "plugin", action: "show", id: "12"}
220 221
  #    assert_recognizes opts, "/plugins/show/12"
  #   end
222
  #
223
  # Note the subtle difference between the two: +assert_routing+ tests that
224
  # a URL fits options while +assert_recognizes+ tests that a URL
225 226 227 228
  # breaks into parameters properly.
  #
  # In tests you can simply pass the URL or named route to +get+ or +post+.
  #
229 230 231 232
  #   def send_to_jail
  #     get '/jail'
  #     assert_response :success
  #   end
233
  #
234 235 236 237
  #   def goes_to_login
  #     get login_url
  #     #...
  #   end
238
  #
239 240
  # == View a list of all your routes
  #
J
Joost Baaij 已提交
241 242 243
  #   rake routes
  #
  # Target specific controllers by prefixing the command with <tt>CONTROLLER=x</tt>.
244
  #
245
  module Routing
246 247 248 249 250 251 252
    extend ActiveSupport::Autoload

    autoload :Mapper
    autoload :RouteSet
    autoload :RoutesProxy
    autoload :UrlFor
    autoload :PolymorphicRoutes
J
Joshua Peek 已提交
253

254
    SEPARATORS = %w( / . ? ) #:nodoc:
255
    HTTP_METHODS = [:get, :head, :post, :patch, :put, :delete, :options] #:nodoc:
256
  end
257
end