简体   繁体   English

将属性从哈希数组打印到Rails中的erb?

[英]Printing properties from an array of hashes to an erb in Rails?

I'm new to Ruby on rails and am a bit confused. 我是Ruby的新手,有些困惑。 I have an array of hashes that I parsed through json in a helper: 我在助手中通过json解析了一系列哈希:

a = [{"desc"=>"testdesc1", "owner"=>{"url"=>"https://www.example1.com"},
{"desc"=>"testdesc2", "owner"=>{"url"=>"https://www.example2.com"}]

and now I want to display it in my .erb like this: 现在我想在我的.erb中显示如下:

<h3>name (with link to url)<h3>
<div>desc</div>

I cannot even begin with the first step, which is to parse the array. 我什至不能从第一步开始,即解析数组。 When I try different variations of the each loop they simply print out the full array. 当我尝试每个循环的不同变体时,它们只是打印出完整的数组。 Is there a straightforward way to do this? 有没有简单的方法可以做到这一点?

Try this: 尝试这个:

array.each do |entry|
    <h3><a href="#{entry["owner"]["url"]}"> entry["name"] </a></h3>
    <div>entry["desc"]</div>
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM