I'm trying to create a simple dropdown with collection_select for some dates, but I am struggling to display the dates in words as times from now (i.e. "One month from today")
我试图用collection_select创建一个简单的下拉菜单,以显示一些日期,但我很难将日期以文字形式显示出来(即从现在开始)。“从今天起一个月”)
So far I have:
到目前为止我有:
def dates_for_review_select
[Date.today, (Date.today + 2.weeks), (Date.today + 1.month), (Date.today + 2.months), (Date.today + 3.months)]
end
and
和
<%= f.collection_select :uk_review_at, dates_for_review_select, :to_s, :to_s, {}, :class => "form-control" %>
(from here -> RoR - collection_select from array)
(从这里-> RoR - collection_select from array)
But I don't know how to change how the dates are displayed. At the moment it is displaying them as normal dates, but I want them to display using the rails "distance_of_time_in_words" helper, so it says how far away the date is in words.
但我不知道如何改变日期的显示方式。目前,它将它们显示为正常的日期,但我希望它们使用rails“distance_of_time_in_words”helper显示,因此它表示日期以单词表示的距离。
But I don't know how or where to implement this? Any ideas?
但是我不知道该如何实现这个?什么好主意吗?
2 个解决方案
#1
3
options_for_select
will take an array of arrays, the first element of each sub array is the display value, the second element is the value returned.
options_for_select将取一个数组,每个子数组的第一个元素是显示值,第二个元素是返回的值。
So this should work...
所以这应该工作…
def dates_for_review_select
[Date.today, (Date.today + 2.weeks), (Date.today + 1.month), (Date.today + 2.months), (Date.today + 3.months)]
end
<%= f.select :uk_review_at, options_for_select(dates_for_review_select.map{|d| [distance_of_time_in_words(Date.today, d), d]}), {}, :class => "form-control review-date" %>
Edited to use the final version that camillavk used.
编辑使用camillavk使用的最终版本。
#2
0
You can improve readability by passing dates_for_review_select
as the collection object, :itself
as the value function, and a lambda
using distance_of_time_in_words
for the text function.
您可以通过将dates_for_review_select作为集合对象:本身作为值函数,以及使用distance_of_time_in_words作为文本函数来提高可读性。
<%= f.collection_select(
:uk_review_at,
dates_for_review_select,
:itself,
-> (t) { distance_of_time_in_words(Date.today, t) },
class: 'form-control review-date') %>
#1
3
options_for_select
will take an array of arrays, the first element of each sub array is the display value, the second element is the value returned.
options_for_select将取一个数组,每个子数组的第一个元素是显示值,第二个元素是返回的值。
So this should work...
所以这应该工作…
def dates_for_review_select
[Date.today, (Date.today + 2.weeks), (Date.today + 1.month), (Date.today + 2.months), (Date.today + 3.months)]
end
<%= f.select :uk_review_at, options_for_select(dates_for_review_select.map{|d| [distance_of_time_in_words(Date.today, d), d]}), {}, :class => "form-control review-date" %>
Edited to use the final version that camillavk used.
编辑使用camillavk使用的最终版本。
#2
0
You can improve readability by passing dates_for_review_select
as the collection object, :itself
as the value function, and a lambda
using distance_of_time_in_words
for the text function.
您可以通过将dates_for_review_select作为集合对象:本身作为值函数,以及使用distance_of_time_in_words作为文本函数来提高可读性。
<%= f.collection_select(
:uk_review_at,
dates_for_review_select,
:itself,
-> (t) { distance_of_time_in_words(Date.today, t) },
class: 'form-control review-date') %>