0

我正在将 Mongodb 与 ROR 一起使用。这是命令:

<%= 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了文件夹是 2 的原因,因为它只需要一次输出,而不管是否。的文件夹。我怎样才能做到这一点?

4

1 回答 1

0

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

所以我用了这个: <%= render partial: "show", object: @h %>

于 2013-09-25T12:32:44.837 回答