.:format 在 rake 路由中是什么意思 [英] What does .:format mean in rake routes

查看:37
本文介绍了.:format 在 rake 路由中是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我输入 rake 路由,然后得到一堆这样的 URL - /articles/:id(.:format)

I type rake routes and I get a bunch of urls like this - /articles/:id(.:format)

我的问题是 - .:format 是什么意思?从 Rails Guides Routing 文章中不清楚,并且在 StackOverflow 或 google 上没有其他有用的 .:format 匹配项.有一个类似的格式是 /:controller(/:action(/:id(.:format))) 我也不明白.

My question is - what does the .:format mean? It is not clear from the Rails Guides Routing article and there are no other helpful matches for .:format on StackOverflow or google. There is a similar format which is /:controller(/:action(/:id(.:format))) which I also don't understand.

谢谢

编辑后续问题 -

如果我只想路由 HTML 页面.在路由中指定类似 .:html 的内容或使用 .:format 并只为 format.html 编写一个 response_to 块是最佳实践吗?在后一种情况下是否会忽略所有其他格式?

If I wanted to only route HTML pages. Would it be best practice to specify something like .:html in the route or to use .:format and just write a respond_to block for format.html? Would all other formats be ignored in that latter case?

推荐答案

这是被请求文件的格式.例如,如果你想要一个图像,你可能在请求中有一个文件扩展名 - 例如,example.com/example_image.png 会给你格式为 png.然后将其包含在请求中,因此您可以根据需要的格式更改响应类型.

That's the format of the file being requested. For instance, if you want an image, you'd probably have a file extension in the request - for instance, example.com/example_image.png would give you the format as png. This is then included in the request so you can vary response type based of of the format requested, need be.

举个例子,您可能希望允许将资源表示为 pdf、纯 html 页面和 json - 您可能会这样写:

For a usage example, you may want to allow a resource to be represented as a pdf, as a plain html page and as json - you'd probably write something like this:

respond_to do |format|
  format.html { ... }
  format.pdf { ... }
  format.json { ... }
end

然后在各自的格式下有单独的渲染调用.

Then have separate render calls under the respective formats.

GET/:controller(/:action(/:id(.:format))) 的说明:controller#:action -

首先,关于格式.括号表示给定的数据是可选的.冒号意味着它在相应的 URL 中找到的任何字符串都应该传递给 params 哈希中的控制器.

First, a bit about formatting. The parentheses mean that a given piece of data is optional. The colon means that whatever string it finds in the corresponding URL should be passed to the controller within the params hash.

这本质上是一个通配符匹配器,它将尝试将大量请求匹配到控制器.例如,假设这是您唯一的路线,并且有人试图获取/users".这会将 users 映射到 UsersController,并默认在其中调用/渲染 index.如果有人得到 users/new,控制器中的 new 动作将被调用.如果 idformat 被调用,它们也会被传递给控制器​​.

This is essentially a wildcard matcher will will attempt to match a very broad number of requests to a controller. For instance, lets say this is your only route, and someone tries to get '/users'. This will map users to the UsersController, and by default call/render index within it. If someone gets users/new, the new action within the controller will be called. If id and format are called, they too will be passed along to the controller.

这篇关于.:format 在 rake 路由中是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆