Ruby-on-rails 在 Rails 3 中,respond_to 和 format.all 的工作方式与 Rails 2 不同吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3672111/
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-02 23:11:11  来源:igfitidea点击:

In Rails 3, respond_to and format.all works differently than Rails 2?

ruby-on-railsruby-on-rails-3mime

提问by nonopolarity

the code

编码

respond_to do |format|
  format.html
  format.json { render :json => @switches }
  format.xml { render :xml => @switches.to_xml }
  format.all { render :text => "only HTML, XML, and JSON format are supported at the moment." }
end

the above will work in Rails 2.2.2. But in Rails 3, getting controller/index.html or index on the browser will both fall into the last line: "only HTML and JSON format are supported at the moment."

以上将在 Rails 2.2.2 中工作。但是在 Rails 3 中,在浏览器上获取 controller/index.html 或 index 都会落入最后一行:“目前仅支持 HTML 和 JSON 格式。”

The only Rails doc I can find on this is

我能找到的唯一 Rails 文档是

http://api.rubyonrails.org/classes/ActionController/MimeResponds/ClassMethods.html#method-i-respond_to

http://api.rubyonrails.org/classes/ActionController/MimeResponds/ClassMethods.html#method-i-respond_to

which current only states:

目前仅声明:

respond_to :html, :xml, :json

but they need separate templates for json and xml, and can't handle the "only HTML and JSON format are supported at the moment" case.

但是他们需要单独的 json 和 xml 模板,并且无法处理“目前仅支持 HTML 和 JSON 格式”的情况。

回答by nathanvda

In rails3 you would write:

在 rails3 你会写:

respond_with(@switches) do |format|
  format.html
  format.json { render :json => @switches }
  format.xml  { render :xml  => @switches }
  format.all  { render :text => "only HTML, XML, and JSON format are supported at the moment." }
end

But this only works in correspondence with a respond_toblock at the top of the file, detailing the expected formats. E.g.

但这仅适用于respond_to文件顶部的块,详细说明了预期的格式。例如

respond_to :xml, :json, :html

Even in that case, if anybody for instance asks the jsformat, the anyblock is triggered.

即使在这种情况下,如果有人询问js格式,any就会触发块。

You could also still use the respond_toalone, as follows:

您也可以respond_to单独使用,如下所示:

@switches = ...
respond_to do |format|
  format.html {render :text => 'This is html'}
  format.xml  {render :xml  => @switches}
  format.json {render :json => @switches}
  format.all  {render :text => "Only HTML, JSON and XML are currently supported"}
end

Hope this helps.

希望这可以帮助。

回答by svilenv

You may find it useful to watch this episode of railscasts, which illustrates the changes to controllers in Rails 3 and in particular the changes to the responder class (putting respond_to in the controller class itself and only using respond_with @object in the action):

您可能会发现观看 Railscast 的这一集很有用,它说明了 Rails 3 中控制器的更改,特别是响应器类的更改(将 respond_to 放在控制器类本身中,并且仅在操作中使用 respond_with @object):

http://railscasts.com/episodes/224-controllers-in-rails-3

http://railscasts.com/episodes/224-controllers-in-rails-3

回答by dreeves

The following works for me. I believe you have to specify the "render" part for html explicitly or it will use the format.any.

以下对我有用。我相信您必须明确指定 html 的“渲染”部分,否则它将使用 format.any。

respond_to do |format|
  format.html { render :html => @switches }
  format.json { render :json => @switches }
  format.xml  { render :xml  => @switches }
  format.all  { render :text => "we only have html, json, and xml" }
end