Ruby-on-rails 如何判断控制器已解析为呈现哪种格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11157234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:35:03  来源:igfitidea点击:

How to tell which format a controller has resolved to render

ruby-on-railsruby-on-rails-3controllerrespond-to

提问by Undistraction

In a rails controller action with the following code:

在带有以下代码的 rails 控制器操作中:

respond_to do |format|
  format.json{ render :json=>  {:status => 200, :response=>@some_resource} }
  format.html { redirect_to(some_resource_path)}
end

How can I log the format the controller will resolve i.e. 'HTML' or 'json'? formatis of type Collector. Is there a way of getting a string denoting the format?

如何记录控制器将解析的格式,即“HTML”或“json”?format是类型Collector。有没有办法获取表示格式的字符串?

回答by Anil

The method to access the format is:

获取格式的方法是:

controller.request.format

回答by localhostdotdev

in your controller you can do:

在您的控制器中,您可以执行以下操作:

request.format
request.format.html?
request.format.js?
request.format.json?
# etc.