Ruby on rails 使用Time.strtime从数组中的多个元素打印日期

Ruby on rails 使用Time.strtime从数组中的多个元素打印日期,ruby-on-rails,ruby,arrays,strptime,Ruby On Rails,Ruby,Arrays,Strptime,我正在尝试使用哈希映射中的数组元素打印格式化日期(records\u array[“START\u TIME\u SEC”]) 数组的第一个元素是:records\u hash[“START\u TIME\u SEC”][0],它等于:1405583947 打印硬编码的时间效果很好: Time.strtime('1405583947','%s')prints:2014-07-17 07:59:07+0000(这是正确的) 接下来,将硬编码时间设置为字符串形式的变量就可以了: stime = '1

我正在尝试使用哈希映射中的数组元素打印格式化日期(
records\u array[“START\u TIME\u SEC”]

数组的第一个元素是:
records\u hash[“START\u TIME\u SEC”][0]
,它等于:1405583947

打印硬编码的时间效果很好:
Time.strtime('1405583947','%s')
prints:2014-07-17 07:59:07+0000(这是正确的)

接下来,将硬编码时间设置为字符串形式的变量就可以了:

stime = '1405583947'
Time.strptime(stime, '%s')
stime2 = records_array["START_TIME_SEC"][0].to_s
Time.strptime(stime2, '%s')
打印:2014-07-17 07:59:07+0000(同样正确!)

如果我从数组中提取一个时间,并尝试在time.strtime中使用它,它就可以正常工作:

stime = '1405583947'
Time.strptime(stime, '%s')
stime2 = records_array["START_TIME_SEC"][0].to_s
Time.strptime(stime2, '%s')
打印:2014-07-17 07:59:07+0000(再次更正)

但是,当尝试遍历数组并打印出所有元素的日期时,我得到一个错误:

<% (0..@records_array.count).each do|i| %>
<tr class="post">   

    <td>
    <% stime = @records_hash["START_TIME_SEC"][i].to_s %>
    <%= Time.strptime(stime, '%s') %>
    </td>
</tr>
<% end %>

错误:
无效的strTime格式-`s'

+1至xlembouras'关于使用
@records_array.each{x |…}
此外,您似乎在代码段上使用了记录\u散列,而不是记录\u数组
这可能是真正的问题。

对于初学者来说,迭代器中有一个错误。你需要
(0..(array.count-1))
但是ruby的方法是
@records\u hash[“START\u TIME\u SEC”]。每个{x |}
你也指的是
@records\u array
@records\u hash
都是对象,还是因为你的另一个代码显示了
@records\u array[“START\u TIME\u SEC”]
.Array.count-1解决了该问题。哇,我觉得自己太傻了,这么长时间都在这上面。非常感谢你!是的,@records\u数组[“START\u TIME\u SEC”]是一个输入错误,它应该是@records\u hash[“START\u TIME\u SEC”]