NameError:未初始化的常量 ActiveRecord::Associations::Builder::XMLMarkup
我发现一篇标题与此非常相似的帖子,但它没有给我我正在寻找的答案。 我正在尝试在模型中使用构建器。代码看起来像这样:
require 'builder'
class Document < ActiveRecord::Base
...
def create_xml
xml = Builder::XmlMarkup.new( :indent => 2)
...
end
...
end
当我尝试执行此代码时,出现以下错误:
NameError: uninitialized constant ActiveRecord::Associations::Builder::XMLMarkup
但是,当我在 Rails 控制台中尝试相同的操作时,一切正常。
我错过了什么吗?任何帮助将不胜感激。
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. 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.
Am I missing something? Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到了答案。
您必须将 Builder 附加到根级别,如下所示:
Found the answer.
You have to append Builder to the rootlevel, like this: