【问题标题】:rails 3: hash attribute in a override to_xml model methodrails 3:覆盖 to_xml 模型方法中的哈希属性
【发布时间】:2014-07-05 03:31:07
【问题描述】:

我为 to_xml 模型方法编写了一个覆盖,现在我试图将一个哈希属性转换为一个集合,如果符号 => 值对,但我没有成功。

intersected_categories 方法的结果是一个像这样的哈希:

{:"1"=>["Broken Board", false], :"2"=>["Sticking", false], :"3"=>["Bottom tier missing", false], :"4"=>["Bad I/L", false], :"5"=>["Delamination", false], :"7"=>["Sheet level - smile", false], :"8"=>["Sheet level - frown", false], :"9"=>["Missing SS", false]}

to_xml 方法

def to_xml (options={})
  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  xml.instruct! unless options[:skip_instruct]
  xml.wip @wip,
  :number => self.number,
  :start_time => self.wip_start_time,
  :end_time => self.wip_end_time,
  :line => self.system.name,
  :shift => self.shift,
  :crew => self.crewF,
  :sm_crew => self.crewSM,
  :sm_shift => self.shiftSM,
  :product => self.SMMainGroup, 
  :product_group => self.product_group, 
  :sheets => self.sheets,
  :interleavers => self.interleavers,
  :wip_grade => self.intersected_categories,
  :comment => self.comment
end

我尝试了几件事但没有成功:

def to_xml (options={})
  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  xml.instruct! unless options[:skip_instruct]
  @wip.intersected_categories.each do |grade|
    wip_categories << {grade[1][0].to_sym => grade[0][1]}
  end
  .
  .
  :comment => self.comment,
  wip_categories #explodes
end

###### another try
  :interleavers => self.interleavers,
  :wip_grade => self.intersected_categories,
  :comment => self.comment,
  self.intersected_categories.each do |grade|
    << grade[1][0].to_sym => grade[0][1] #with / without {} it keeps failing
  end
end

知道如何实现我的需要吗?

更新

当前xml:

 <wip number="123h11008" start_time="2014-05-13 09:21:00 -0500" end_time="" line="cl1" shift="1" crew="A" sm_crew="A" sm_shift="1" product="1111111" product_group="G2" sheets="0" interleavers="0" wip_grade="{:"2"=>["Bottom Tier Missing", false], :"3"=>["Falling", false], :"4"=>["Broken Boards", false], :"5"=>["Crown", false], :"6"=>["DIP", false], :"7"=>["Green Boards", false], :"8"=>["Delamination", false], :"9"=>["Bad I/L", false]}" comment=""/>

预期的 xml:

 <wip number="123h11008" start_time="2014-05-13 09:21:00 -0500" end_time="" line="cl1" shift="1" crew="A" sm_crew="A" sm_shift="1" product="1111111" product_group="G2" sheets="0" interleavers="0" Bottom Tier Missing=false Falling =  false Broken Boards = false Crown = false DIP = false Green Boards = false Delamination = false Bad I/L = false comment=""/>

【问题讨论】:

  • 你能展示一下预期的结果吗?

标签: ruby-on-rails ruby ruby-on-rails-3 hash xml-serialization


【解决方案1】:

试试:

xml.wip @wip, {
 :number => self.number,
 :start_time => self.wip_start_time,
 :end_time => self.wip_end_time,
 :line => self.system.name,
 :shift => self.shift,
 :crew => self.crewF,
 :sm_crew => self.crewSM,
 :sm_shift => self.shiftSM,
 :product => self.SMMainGroup, 
 :product_group => self.product_group, 
 :sheets => self.sheets,
 :interleavers => self.interleavers,
 :comment => self.comment }.merge(Hash(self.intersected_categories.values))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-25
    • 2014-02-17
    • 1970-01-01
    相关资源
    最近更新 更多