Rails 3.1 Edge 是否破坏了 XmlMarkup::Builder?

发布于 2024-11-06 14:24:57 字数 1205 浏览 0 评论 0原文

网上有很多示例(例如 http ://techoctave.com/c7/posts/32-create-an-rss-feed-in-rails)展示了如何使用 Builder 制作漂亮的 RSS 提要。规范模板是这样的:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
xml.channel do
  xml.title "Your Blog Title"
  xml.description "A blog about software and chocolate"
  xml.link posts_url

  for post in @posts
    xml.item do
      xml.title post.title
      xml.description post.content
      xml.pubDate post.posted_at.to_s(:rfc822)
      xml.link post_url(post)
      xml.guid post_url(post)
    end
  end
end

这在 Rails 3.0.7 中工作得很好。在 Rails 3.1 Edge 中,每个命令似乎都会产生...

Rendered home/index.rss.builder (25.2ms)
Completed 500 Internal Server Error in 875ms

ActionView::Template::Error (wrong number of arguments (1 for 0)):
1: xml.instruct!(:xml, :encoding => "UTF-8")
2: 
3: xml.rss :version => "2.0" do
4:   xml.channel do
app/views/home/index.rss.builder:1:in   `_app_views_home_index_rss_builder___2123990471_2215695900'
app/controllers/home_controller.rb:17:in `index'
app/controllers/home_controller.rb:11:in `index'

There are a number of examples on the Web (such as http://techoctave.com/c7/posts/32-create-an-rss-feed-in-rails) showing how to make a nice RSS feed using Builder. The canonical template is something like this:

xml.instruct! :xml, :version => "1.0" 
xml.rss :version => "2.0" do
xml.channel do
  xml.title "Your Blog Title"
  xml.description "A blog about software and chocolate"
  xml.link posts_url

  for post in @posts
    xml.item do
      xml.title post.title
      xml.description post.content
      xml.pubDate post.posted_at.to_s(:rfc822)
      xml.link post_url(post)
      xml.guid post_url(post)
    end
  end
end

This works fine in Rails 3.0.7. In Rails 3.1 Edge, every command seems to produce...

Rendered home/index.rss.builder (25.2ms)
Completed 500 Internal Server Error in 875ms

ActionView::Template::Error (wrong number of arguments (1 for 0)):
1: xml.instruct!(:xml, :encoding => "UTF-8")
2: 
3: xml.rss :version => "2.0" do
4:   xml.channel do
app/views/home/index.rss.builder:1:in   `_app_views_home_index_rss_builder___2123990471_2215695900'
app/controllers/home_controller.rb:17:in `index'
app/controllers/home_controller.rb:11:in `index'

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

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

发布评论

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

评论(2

ゃ懵逼小萝莉 2024-11-13 14:24:57

Rails 3.1.0.rc1 也因这个错误而对我造成了影响,但仅在使用 Ruby 1.8.7 时 - 事实证明这是 .instruct! 的问题

作为临时解决方案,您可以使用 Monkeypatch xchar.rb以下(根据这篇文章的作者的建议http://lists.alioth.debian.org/pipermail/pkg- ruby-extras-maintainers/2010-June/005411.html):

--- /home/prahal/xmlbase.rb.orig  2010-06-03 11:18:38.000000000 +0200
+++ /home/prahal/xmlbase.rb.new 2010-06-03 11:18:53.000000000 +0200
@@ -131,7 +131,11 @@
       end
     else
       def _escape(text)
-        text.to_xs((@encoding != 'utf-8' or $KCODE != 'UTF8'))
+        begin
+   text.to_xs((@encoding != 'utf-8' or $KCODE != 'UTF8'))
+        rescue
+   text.to_xs()
+        end
       end
     end

Rails 3.1.0.rc1 broke for me with this bug too, but only when using Ruby 1.8.7 - turns out it's a problem with .instruct!

As a temporary solution you could monkeypatch xchar.rb with the following (as suggested by the author of this post http://lists.alioth.debian.org/pipermail/pkg-ruby-extras-maintainers/2010-June/005411.html):

--- /home/prahal/xmlbase.rb.orig  2010-06-03 11:18:38.000000000 +0200
+++ /home/prahal/xmlbase.rb.new 2010-06-03 11:18:53.000000000 +0200
@@ -131,7 +131,11 @@
       end
     else
       def _escape(text)
-        text.to_xs((@encoding != 'utf-8' or $KCODE != 'UTF8'))
+        begin
+   text.to_xs((@encoding != 'utf-8' or $KCODE != 'UTF8'))
+        rescue
+   text.to_xs()
+        end
       end
     end
策马西风 2024-11-13 14:24:57

如果您同时安装了 Builder 3.0 和 fast_xs 0.8.0(请注意,hpricot 还捆绑了 fast_xs 0.8.0),您也会收到此错误。

您可以使用 application.rb 中的以下猴子补丁来解决此 问题:

class String
  alias_method :orig_fast_xs, :fast_xs
  def fast_xs(ignore)
    orig_fast_xs
  end
end

You will also get this error if you have both Builder 3.0 and fast_xs 0.8.0 installed (note that hpricot bundles fast_xs 0.8.0 also)

You can work around this with the following monkey patch in application.rb:

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