渲染在轨道与集合打印三次

时间:2013-09-25 10:56:17

标签: ruby-on-rails render renderpartial mongoid3

我正在使用带有ROR的Mongodb。这是命令:

<%= render partial: "show", collection: @h %>

@h

的位置
@h=[
  {"folder"=>"Default", "weight"=>"100.0", "stocks"=>[
    {"id"=>"id1", "name"=>"Ajanta Pharma Ltd.", "weight"=>"57.0"},
    {"id"=>"id11", "name"=>"Avanti Feeds Ltd.", "weight"=>"5.1"}, 
    {"id"=>"id34", "name"=>"MPS Ltd.", "weight"=>"2.5"}, 
    {"id"=>"id89", "name"=>"Poly Medicure Ltd.", "weight"=>"3.7"}]}, 
  {"folder"=>"low margin", "weight"=>"0.0", "stocks"=>[
    ]}, 
  {"folder"=>"fjf", "weight"=>"0.0", "stocks"=>[
  ]}
] 

假设我的_show.erb就是这个

i am line

我得到的输出i am linei am linei am line实际上没有。 @h * _show.erb内容中的文件夹的时间@h如果我将@h=[ {"folder"=>"Default", "weight"=>"100.0", "stocks"=>[ {"id"=>"id1", "name"=>"Ajanta Pharma Ltd.", "weight"=>"57.0"}, {"id"=>"id11", "name"=>"Avanti Feeds Ltd.", "weight"=>"5.1"}, {"id"=>"id34", "name"=>"MPS Ltd.", "weight"=>"2.5"}, {"id"=>"id89", "name"=>"Poly Medicure Ltd.", "weight"=>"3.7"}]}, {"folder"=>"low margin", "weight"=>"0.0", "stocks"=>[ ]}` ] 更改为此

i am linei am line

然后它给{{1}}作为文件夹是2为什么它发生,因为只想要一次输出而不管是否。的文件夹。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

完成,实际当您通过:collection选项将集合传递给partial时,将为集合中的每个成员插入一次partial:

所以我使用了这个:&lt;%= render partial:“show”,object:@h%&gt;