Rails 控制器处理为 HTML 而不是 XML

发布于 2024-10-12 06:01:34 字数 987 浏览 1 评论 0原文

我最近从 Ruby 1.8.6 和 Rails 2.3.4 升级到 Ruby 1.9 和 Rails 3.0.3。

我有以下控制器:

class ChartController < ApplicationController

  before_filter :login_required

  respond_to :html, :xml  

  def load_progress  

    chart.add( :series, "Memorized",  y_memorized )
    chart.add( :series, "Learning",   y_learning  )
    chart.add( :series, "Mins / Day", y_time      )  
    chart.add( :user_data, :secondary_y_interval, time_axis_interval )

    respond_to do |fmt|
      fmt.xml { render :xml => chart.to_xml }
    end

    # Also tried
    # respond_with chart

  end   
end

但是,当我调用“load_progress 方法”时,我得到以下信息:

Started GET "/load_progress.xml" for 127.0.0。 由 ChartController#load_progress 作为 HTML 处理 Completed 406 Not Acceptable in 251ms

我也尝试将 respond_to 块更改为

respond_with chart

但我得到了相同的响应。我已经阅读了有关新的 respond_with 格式的所有新 Rails 文档,但我似乎无法引出 XML 响应。我迫切希望有人有一些想法。

I've recently upgraded from Ruby 1.8.6 and Rails 2.3.4 to Ruby 1.9 and Rails 3.0.3.

I have the following controller:

class ChartController < ApplicationController

  before_filter :login_required

  respond_to :html, :xml  

  def load_progress  

    chart.add( :series, "Memorized",  y_memorized )
    chart.add( :series, "Learning",   y_learning  )
    chart.add( :series, "Mins / Day", y_time      )  
    chart.add( :user_data, :secondary_y_interval, time_axis_interval )

    respond_to do |fmt|
      fmt.xml { render :xml => chart.to_xml }
    end

    # Also tried
    # respond_with chart

  end   
end

However, when I call the 'load_progress method' I get the following:

Started GET "/load_progress.xml" for 127.0.0.
Processing by ChartController#load_progress as HTML
Completed 406 Not Acceptable in 251ms

I have also tried changing the respond_to block to

respond_with chart

But I get the same response. I've read all the new Rails documentation on the new respond_with format but I can't seem to elicit an XML response. Am desperately hoping someone has some ideas.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年不长 2024-10-19 06:01:34

我遇到了同样的问题,以下代码片段对我有用:

  respond_to :xml
  def list
    @items = Item.all
    render :xml => @items
  end

406 可能因多种原因而发生 - 通常当人们使用错误的 MIME 类型时 - 但基于 rails 指南 XML 响应如上所述,所有内容都将由rails 正确填写。

上面的代码片段有一个缺点。它将列出模型的每个属性。

在您的示例中,我不确定 chart 变量是否已初始化/可见。

I had the same issue, and the following snippet worked for me:

  respond_to :xml
  def list
    @items = Item.all
    render :xml => @items
  end

406 can happen for several reasons - usually when one uses wrong MIME types -, but based on the rails guides when you create an XML response as described above, everything will be filled out correctly by rails.

There is one disadvantage of the above snippet. It will list every attribute of your model.

In your example I'm not sure whether the chart variable is initialized/visible or not.

嘿哥们儿 2024-10-19 06:01:34

你正在做的事情看起来是正确的。您使用的是 Ruby 1.9.2 吗?我知道 1.9.0 有问题,但我不确定它能解释这个问题。

What you are doing looks right. Are you using Ruby 1.9.2? I know that 1.9.0 has problems, but I'm not sure it would explain this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文