igo*_*_rb 14 rake routes ruby-on-rails-3
'输出(.:format)意味着什么rake routes?
users GET /users(.:format) users#index
Run Code Online (Sandbox Code Playgroud)
Ami*_*ait 31
如果你检查你的index动作Users Controller那么你会看到这样的事情
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users }
end
end
Run Code Online (Sandbox Code Playgroud)
因此,此格式是将生成的响应类型.
在路径中,无论在控制器的操作中定义了什么格式,都会创建响应类型的占位符.
所以,如果你的网址是这样的: -
users GET /users --> users/index.html.erb will be rendered
users GET /users.json --> users/index.json.erb will be rendered
Run Code Online (Sandbox Code Playgroud)
类似地,如果您想要响应PDF或xls格式化,那么您只需要定义format.pdf或者format.xls您还必须定义这些新MIME类型,这些类型在某些初始化文件中的rails中默认不存在.
那么,如果请求如下: -
users GET /users.xls --> users/index.xls.erb will be rendered
Run Code Online (Sandbox Code Playgroud)
然后,您的路径文件将仅format.xls在索引操作中查找,users/index.xls.erb并将呈现相应的视图文件方式.
| 归档时间: |
|
| 查看次数: |
2463 次 |
| 最近记录: |