Rails 3.1 Edge打破XmlMarkup :: Builder?

时间:2011-05-10 17:49:57

标签: ruby ruby-on-rails-3.1

网上有很多例子(例如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'

2 个答案:

答案 0 :(得分:6)

Rails 3.1.0.rc1也为我带来了这个错误,但只有在使用Ruby 1.8.7时才会发现它是.instruct!

的问题

作为一个临时解决方案,您可以使用以下内容(如本帖子作者http://lists.alioth.debian.org/pipermail/pkg-ruby-extras-maintainers/2010-June/005411.html的建议)monkeypatch xchar.rb:

--- /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

答案 1 :(得分:3)

如果你同时安装了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