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

和自甴很熟 提交于 2019-11-30 08:34:57

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!