【问题标题】:set a rails controller's response type to xml将 rails 控制器的响应类型设置为 xml
【发布时间】:2012-09-28 01:58:02
【问题描述】:

我对 Rails 很陌生。我正在尝试将 Rails 控制器的响应类型设置为 xml,但运气不佳。我当然可以更好地理解 respond_to 和 respond_with 是如何工作的。

这是我的控制器的样子:

class ResponsesController < ApplicationController

 respond_to :xml

  def index

    require 'rubygems'   
    require 'telapi'

        ix = Telapi::InboundXml.new do

          Say('Hello.', :loop => 3, :voice => 'man')
          Say('Hello, my name is Jane.', :voice => 'woman')
          Say('Now I will not stop talking.', :loop => 0)
        end

        respond_with do |format|
            format.xml { render }
        end

        puts ix.response 

    end
end

这会导致 http 检索失败。有人可以告诉我如何修复控制器并将其响应类型设置为 xml 吗?此外,关于 respond_to 和 respond_with 如何工作的令人信服的 1-2 班轮会很棒!

谢谢大家。

【问题讨论】:

  • 尝试 format.xml { 渲染 xml: ix }

标签: ruby-on-rails respond-to respond-with


【解决方案1】:

替换

  respond_with do |format|
            format.xml { render }
        end

respond_with(ix)

有 2 种渲染 xml 的方法。示例 1 使用了 respond_to,这意味着“每个方法都将使用 xml 并使用从 respond_with 中解析的对象”

示例 2 使用了 respond_to,意思是“使用下面的块来声明什么类型的响应和要解析的对象”

示例 1:

class ResponsesController
  respond_to :xml #respond_to A

  def index
    respond_with(@asd) # respond_with A
  end
end

示例 2:

def ResponsesController

  def index
    respond_to do |format|
     format.xml { render xml: @asd}
    end
  end
end

http://blog.plataformatec.com.br/2009/08/embracing-rest-with-mind-body-and-soul/

【讨论】:

  • 感谢您的帮助。我最终使用了类似的东西,即 format.xml { render :xml => xml.response }
猜你喜欢
  • 2018-10-05
  • 2017-08-08
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-20
  • 1970-01-01
  • 2021-04-20
相关资源
最近更新 更多