Rails 控制器处理为 HTML 而不是 XML
我最近从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,以下代码片段对我有用:
406 可能因多种原因而发生 - 通常当人们使用错误的 MIME 类型时 - 但基于 rails 指南 XML 响应如上所述,所有内容都将由rails 正确填写。
上面的代码片段有一个缺点。它将列出模型的每个属性。
在您的示例中,我不确定
chart
变量是否已初始化/可见。I had the same issue, and the following snippet worked for me:
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.你正在做的事情看起来是正确的。您使用的是 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.