【发布时间】:2015-05-27 23:05:52
【问题描述】:
我正在尝试使用辅助方法构建一个哈希数组(我认为这就是我的表述方式),以便我可以在我的视图中使用它。我从@other_events.time_start 和@other_events.time_end 列中获取2 个值。
helper.rb
def taken_times()
@taken_times = []
@other_events.each do |e|
@taken_times << { e.time_start.strftime("%l:%M %P") => e.time_end.strftime("%l:%M %P")}
end
@taken_times
end
我想要的是一个这样的哈希数组:
['10:00am', '10:15am'],
['1:00pm', '2:15pm'],
['5:00pm', '5:15pm'],
本质上是
['e.time_start', 'e.time_end'],
【问题讨论】:
-
你想要一个简单的数组。哈希数组看起来
[{time_start: '10:00am', time_end: '10:15am'},{time_start: '1:00pm', time_end: '2:15pm'}] -
您的示例输出不是哈希数组,而是数组数组。不过,您基本上已经明白了……您遇到了什么问题?
-
它甚至不是一个数组数组——它是三个数组,它们之间有逗号。如果它是一个数组数组,它将被括在另一对方括号中。
标签: ruby-on-rails arrays ruby-on-rails-4 hash helper