使用acts_as_tree的完全限定树的to_xml
我有一个使用acts_as_tree 的ActiveRecord 类。我正在尝试更新 to_xml 方法,以便如果调用子记录的 to_xml ,它将返回嵌套在父/祖先 xml 中的 xml,以提供该资源的完全限定路径。作为一个例子,我有编译器,它是编译器/版本的父级。编译器应将其呈现为 xml:
虽然编译器/版本应呈现为
我尝试通过传递 full_qualified 标志来做到这一点,但它因 'Builder::XmlMarkup#to_ary should return Array'
def to_xml(options={}, &block) 而终止 选项[:完全限定] ||= true 选项[:缩进] ||= 2 options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
if options[:fully_qualified] and not parent.nil?
55:parent.to_xml(options) do |foo| 相对选项=选项.dup 相对选项[:完全限定] = false 相对选项[:跳过指令] = true relative_options.delete(:builder)
foo << to_xml(relative_options)
end
else
xml = options[:builder]
xml.instruct! unless options[:skip_instruct]
66: xml.parameter(:name => name, &block) 结尾 该
方法对于编译器的情况工作正常,但对于编译器/版本失败:
/usr/lib/ruby/1.8/builder/xmlbase.rb:133:in method_missing' /usr/lib/ruby/1.8/builder/xmlbase.rb:133:in
调用' /usr/lib/ruby/1.8/builder/xmlbase.rb:133:in _nested_structs' /usr/lib/ruby/1.8/builder/xmlbase.rb:57:in
method_missing' app/models/parameter.rb:66:in to_xml' app/models/parameter.rb:55:in
to_xml'
I have an ActiveRecord class that uses acts_as_tree. I'm trying to update the to_xml method so that if a child record's to_xml is called it will return it's xml nested in the parent/ancestor xml to give the fully-qualified path to that resource. As an example I have Compiler which is a parent of Compiler/Version. Compiler should be rendered as xml:
While Compiler/Version should render as
I've tried to do this by passing around a fully_qualified flag, but it dies with 'Builder::XmlMarkup#to_ary should return Array'
def to_xml(options={}, &block)
options[:fully_qualified] ||= true
options[:indent] ||= 2
options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
if options[:fully_qualified] and not parent.nil?
55: parent.to_xml(options) do |foo|
relative_options = options.dup
relative_options[:fully_qualfied] = false
relative_options[:skip_instruct] = true
relative_options.delete(:builder)
foo << to_xml(relative_options)
end
else
xml = options[:builder]
xml.instruct! unless options[:skip_instruct]
66: xml.parameter(:name => name, &block)
end
end
The method works fine for the case of Compiler, but fails for Compiler/Version:
/usr/lib/ruby/1.8/builder/xmlbase.rb:133:in method_missing'
call'
/usr/lib/ruby/1.8/builder/xmlbase.rb:133:in
/usr/lib/ruby/1.8/builder/xmlbase.rb:133:in _nested_structures'
method_missing'
/usr/lib/ruby/1.8/builder/xmlbase.rb:57:in
app/models/parameter.rb:66:in to_xml'
to_xml'
app/models/parameter.rb:55:in
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来您无法在任何单一关联上调用 to_xml 。父级和参数上的 to_xml (has_one 关系)失败,但如果我使用 find_by_id 进行查找,它会起作用:
def to_xml(options={}, &block)
my_options = 选项.dup
my_options[:全限定] = true 除非 my_options.has_key?(:全限定)
my_options[:only] = [:name]
结束
It appears that you can't call to_xml on any singular-association. to_xml on parent and parameter (both has_one relationships) failed, but if I did the lookup with find_by_id it worked:
def to_xml(options={}, &block)
my_options = options.dup
my_options[:fully_qualified] = true unless my_options.has_key?(:fully_qualified)
my_options[:only] = [:name]
end