Investigation into Rails routing
I wanted to understand how combinations of resources :format
and resources :constraints
affected the HTTP response codes from Rails.
I created a new Rails 4.1.8 app and used scaffolding (rails g scaffold people name:string
) to create a very simple app.
I started the Rails server and created a single person (with ID 1).
I updated the Routes and recorded the results of a request to GET the Person using cURL.
Resources
# In routes.rb
# resources :people
$ curl http://localhost:3000/people/1
#=> 200 html
$ curl http://localhost:3000/people/1a
#=> 200 html
$ curl http://localhost:3000/people/1.html
#=> 200 html
$ curl http://localhost:3000/people/1a.html
#=> 200 html
$ curl http://localhost:3000/people/1.png
#=> 500 Missing Template
$ curl http://localhost:3000/people/1a.png
#=> 500 Missing Template
Resources with format not allowed
# In routes.rb
# resources :people, format: false
$ curl http://localhost:3000/people/1
#=> 200 html
$ curl http://localhost:3000/people/1a
#=> 200 html
$ curl http://localhost:3000/people/1.html
#=> 404 Routing Error
$ curl http://localhost:3000/people/1a.html
#=> 404 Routing Error
$ curl http://localhost:3000/people/1.png
#=> 404 Routing Error
$ curl http://localhost:3000/people/1a.png
#=> 404 Routing Error
Resources with format required
# In routes.rb
# resources :people, format: true
$ curl http://localhost:3000/people/1
#=> 404 Routing error
$ curl http://localhost:3000/people/1a
#=> 404 Routing error
$ curl http://localhost:3000/people/1.html
#=> 200 html
$ curl http://localhost:3000/people/1a.html
#=> 200 html
$ curl http://localhost:3000/people/1.png
#=> 500 Missing Template
$ curl http://localhost:3000/people/1a.png
#=> 500 Missing Template
Resources with format set to html
# In routes.rb
# resources :people, format: :html
$ curl http://localhost:3000/people/1
#=> 200 html
$ curl http://localhost:3000/people/1a
#=> 200 html
$ curl http://localhost:3000/people/1.html
#=> 200 html
$ curl http://localhost:3000/people/1a.html
#=> 200 html
$ curl http://localhost:3000/people/1.png
#=> 500 Missing Template
$ curl http://localhost:3000/people/1a.png
#=> 500 Missing Template
Resources with constraint forcing numeric IDs
# In routes.rb
# resources :people, constraints: {id: /\d+/}
$ curl http://localhost:3000/people/1
#=> 200 html
$ curl http://localhost:3000/people/1a
#=> 404 Routing Error
$ curl http://localhost:3000/people/1.html
#=> 200 html
$ curl http://localhost:3000/people/1a.html
#=> 404 Routing Error
$ curl http://localhost:3000/people/1.png
#=> 500 Missing Template
$ curl http://localhost:3000/people/1a.png
#=> 404 Routing Error
Resources with constraint forcing numeric IDs and no Format
# In routes.rb
# resources :people, constraints: {id: /\d+/, format: //}
$ curl http://localhost:3000/people/1
#=> 200 html
$ curl http://localhost:3000/people/1a
#=> 404 Routing Error
$ curl http://localhost:3000/people/1.html
#=> 404 Routing Error
$ curl http://localhost:3000/people/1a.html
#=> 404 Routing Error
$ curl http://localhost:3000/people/1.png
#=> 404 Routing Error
$ curl http://localhost:3000/people/1a.png
#=> 404 Routing Error
Resources with constraint forcing numeric IDs and either html or no Format
# In routes.rb
# resources :people, constraints: {id: /\d+/, format: /html/}
$ curl http://localhost:3000/people/1
#=> 200 html
$ curl http://localhost:3000/people/1a
#=> 404 Routing Error
$ curl http://localhost:3000/people/1.html
#=> 200 html
$ curl http://localhost:3000/people/1a.html
#=> 404 Routing Error
$ curl http://localhost:3000/people/1.png
#=> 404 Routing Error
$ curl http://localhost:3000/people/1a.png
#=> 404 Routing Error