Ruby-on-rails 如何在 Rails 中设置路由的默认格式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4800219/
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
How to set the default format for a route in Rails?
提问by Saucerful
With the default routing, the request /posts/:id gets mapped to the "show" action with :format => "html". I am using some xhtml elements in my show action which don't get rendered correctly unless the :content_type is set to xml. I am currently getting around this by rendering show.xml.erb and setting the content_type manually as follows:
使用默认路由,请求 /posts/:id 被映射到带有:format => "html". 我在我的 show 操作中使用了一些 xhtml 元素,除非 :content_type 设置为 xml,否则这些元素不会正确呈现。我目前正在通过渲染 show.xml.erb 并手动设置 content_type 来解决这个问题,如下所示:
format.html { render :template => "/posts/show.xml.erb",
:locals => {:post => @post}, :content_type => "text/xml" }
This seems silly though. How can I change routes.rbso that /posts/:id is routed with format=>"xml"? Thanks.
虽然这看起来很傻。如何更改routes.rb以便 /posts/:id 与路由format=>"xml"?谢谢。
回答by tomeduarte
Default format for requests:
请求的默认格式:
You can set the default format of a given route to xml using the defaults hash.
您可以使用默认哈希将给定路由的默认格式设置为 xml。
Examples:
例子:
# single match defaulting to XML (/plots/1 is the same as /plots/1.xml)
match 'posts/:id' => 'posts#show', :defaults => { :format => 'xml' }
# using resources, defaulting to XML (all action use XML by default)
resources :posts, :defaults => {?:format => 'xml' }
# using resources and mixing with other options
resources :posts,
:only => [:new, :create, :destroy],
:defaults => { :format => 'xml' }
It's always a good idea to search the official Ruby on Rails routing guide, it's fairly in-depth and a very good first-stop resource for any routing issues.
搜索官方的 Ruby on Rails 路由指南总是一个好主意,它相当深入,是解决任何路由问题的非常好的第一站资源。
回答by Jimmy Cuadra
If you only want to support one format and treat all requests as that format, you could use a filter to change it:
如果您只想支持一种格式并将所有请求视为该格式,您可以使用过滤器来更改它:
before_filter :set_format
def set_format
request.format = 'xml'
end
回答by Tombart
Rails 4 and 5: In your controller (e.g. ApplicationControllerif all whole application uses same format) add following:
Rails 4 和 5:在您的控制器中(例如,ApplicationController如果所有整个应用程序都使用相同的格式)添加以下内容:
before_action :set_default_request_format
def set_default_request_format
request.format = :json unless params[:format]
end
For Rails 3and older use before_filterinstead of before_action.
对于Rails的3岁以上使用before_filter的替代before_action。
回答by zachaysan
I'm finding weird behaviour in Rails 5 if you use this:
如果你使用这个,我会在 Rails 5 中发现奇怪的行为:
{ format: :json }
In your config/routes.rbthen even if JSON isn't set in your accept header, it still coerces the request to a JSON request, including for controller tests that have the as: :htmloption set. It's not really a big deal for me, so I'm not going to dig into why this is, but if someone figures it out, let me know and I'll update this answer.
在您的config/routes.rbthen 中,即使未在您的接受标头中设置 JSON,它仍会将请求强制转换为 JSON 请求,包括as: :html设置了该选项的控制器测试。这对我来说并不是什么大问题,所以我不会深入研究为什么会这样,但是如果有人弄清楚了,请告诉我,我会更新此答案。
回答by Son Dang
If you want to set the default format for a route, use defaultsoption:
如果要设置路由的默认格式,请使用defaults选项:
resources :posts, defaults: { format: 'xml' }
But if you want to enforce every request to return a specific format, use constraintsoption:
但是,如果您想强制每个请求返回特定格式,请使用constraints选项:
resources :posts, constraints: lambda { |req| req.format == 'xml' }
See the documentation: http://edgeguides.rubyonrails.org/routing.html#request-based-constraints
请参阅文档:http: //edgeguides.rubyonrails.org/routing.html#request-based-constraints

