[英]NameError: uninitialized constant ActiveRecord::Associations::Builder::XMLMarkup
I found a post with a headline quite similar to this one, but it didn't give me the answer I was looking for. 我发现一个标题与此标题非常类似的帖子,但它没有给我我想要的答案。 I am trying to use builder inside a model. 我试图在模型中使用builder。 The code looks something like this: 代码看起来像这样:
require 'builder'
class Document < ActiveRecord::Base
...
def create_xml
xml = Builder::XmlMarkup.new( :indent => 2)
...
end
...
end
When I try to execute this code, I get the following error: 当我尝试执行此代码时,出现以下错误:
NameError: uninitialized constant ActiveRecord::Associations::Builder::XMLMarkup
But, when I try the same thing within the rails console, everything works just fine. 但是,当我在rails控制台中尝试相同的操作时,一切正常。
Am I missing something? 我错过了什么吗? Any help would be much appreciated. 任何帮助将非常感激。
Found the answer. 找到了答案。
You have to append Builder to the rootlevel, like this: 您必须将Builder附加到根级别,如下所示:
xml = ::Builder::XmlMarkup.new( :indent => 2 )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.