(.:format)
是什么意思rake routes
中的平均值' 输出?
users GET /users(.:format) users#index
最佳答案
如果您查看 index
您的 Users Controller
的操作然后你会看到这样的东西
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
因此,这种格式就是将要生成的响应类型。
在路由中,无论 Controller 的操作中定义了何种格式,都会创建响应类型的占位符。
因此,如果您的 URL 类似于:-
users GET /users --> users/index.html.erb will be rendered
users GET /users.json --> users/index.json.erb will be rendered
同样,如果您想在
PDF
中回复或 xls
格式,那么你只需要定义 format.pdf
或 format.xls
你还必须定义这些新的MIME
在某些初始化程序文件中,rails 中默认不存在的类型。因此,如果提出如下请求:-
users GET /users.xls --> users/index.xls.erb will be rendered
然后,您的路线文件将只查找
format.xls
在索引操作和相应的 View 文件中表示 users/index.xls.erb
将被渲染。
关于ruby-on-rails-3 - 什么是(。:format) mean in rake routes' output?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17612233/