Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does (.:format) mean in rake routes' output?

What does (.:format) mean in rake routes' output?

users GET    /users(.:format)          users#index
like image 929
igor_rb Avatar asked Jul 12 '13 09:07

igor_rb


People also ask

How does routes work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

What is RESTful routes in Rails?

The Rails router is responsible for redirecting incoming requests to controller actions. The routing module provides URL rewriting in native Ruby. It recognizes URLs and dispatches them as defined in config/routes.

What is a Route in ruby on Rails?

Ruby on Rails routes are essential to a Rails application's function. The Rails routing system checks the URLs of incoming requests and decides what action or actions the application should take in response.


1 Answers

If you check the index action of your Users Controller then you will see something like this

def index
  @users = User.all

  respond_to do |format|
    format.html # index.html.erb
    format.json { render json: @users }
  end
end

So, this format is the type of response which will be generated.

In routes, a placeholder for the type of response is created irrespective of whatever format has been defined in the action of the controller.

So, if your URL is something like :-

users GET /users        --> users/index.html.erb will be rendered
users GET /users.json   --> users/index.json.erb will be rendered

Similarly, if you want response in PDF or xls format, then you just have to define format.pdf or format.xls and also you have to define these new MIME types which are not there by default in rails in some initializer file.

So, then if a request is made like :-

users GET /users.xls     --> users/index.xls.erb will be rendered

Your routes file will then just look for the format.xls in the index action and respective view file means users/index.xls.erb will be rendered.

like image 92
Amit Thawait Avatar answered Oct 12 '22 16:10

Amit Thawait