Jonathan Chad Faling
Jonathan Chad Faling

Reputation: 1479

Please explain what (.:format) means

In ruby on rails when I run 'rake routes' the output for example is

                Prefix Verb   URI Pattern                           Controller#Action
                 users GET    /users(.:format)                      users#index
                       POST   /users(.:format)                      users#create

what does the (.:format) mean?

Upvotes: 1

Views: 252

Answers (2)

Mario Uher
Mario Uher

Reputation: 12397

  • Parentheses inside a route define an optional route segment.
  • A colon followed by a name (in this case :format) defines a variable, which can be accessed later in your controller via the params hash.

Depending on your controller, your users can be retrieved in multiple formats in this particular example (e.g. /users.json and /users.xml). When the format variable is omitted (e.g. accessing /users), Rails defaults to the HTML format.

Be sure to check the offical Rails Guides and especially the sections 3.1 Bound Parameters and 3.2 Dynamic Segments for more information.

Upvotes: 2

apchester
apchester

Reputation: 1144

The format refers the the data format being requested. This might be JSON or XML, so your route would match:

/users.json or /users.xml

Leaving this blank, gives the HTML version.

Upvotes: 1

Related Questions