Ruby on Rails - 在助手中仅显示当前和未来月份
Ruby on Rails - Show only current and future months in helper
我有这个助手,可以在 select 输入中显示事件模型中的月份和年份。我如何只显示当前和未来的月份而不显示过去的月份?
def select_month_tag(events)
html = <<-HTML
<select id="filtro-mes-ano" class="event-filter selectCustom2 event_filter_select">
<option value="Filtrar por mês" disabled selected >Filtrar por mês</option>
HTML
events.each do | event |
if not date_and_month(event.month_ref, event.year_ref).blank?
html += <<-HTML
<option data-year="#{event.year_ref}"
data-month="#{event.month_ref}"
"#{'selected' if is_hash_selected?(event)}">
date_and_month(event.month_ref, event.year_ref)}
</option>
HTML
end
end
html += <<-HTML
</select>
HTML
html.html_safe
end
谢谢。
我猜你会为你编写的那种代码做这样的事情。
让我知道它是否适合你
def select_month_tag(events)
html = <<-HTML
<select id="filtro-mes-ano" class="event-filter selectCustom2 event_filter_select">
<option value="Filtrar por mês" disabled selected >Filtrar por mês</option>
HTML
events.each do | event |
if not date_and_month(event.month_ref, event.year_ref).blank?
# Get today's date
today = Date.today
# check if event year is greater than present date's year
if event.year_ref >= today.year
# check if event month is greater than present date's month
if event.month_ref >= today.month # considering month_ref is a number between 1..12
html += <<-HTML
<option data-year="#{event.year_ref}"
data-month="#{event.month_ref}"
"#{'selected' if is_hash_selected?(event)}">
date_and_month(event.month_ref, event.year_ref)}
</option>
HTML
end
end
end
end
html += <<-HTML
</select>
HTML
html.html_safe
end
为什么不过滤 Event
集合,然后用它创建选项?我认为您不希望助手中有这么多逻辑。最好在模型上做一个范围,然后使用它。像
Event.where('date > ?', date_variable_here)
Can you do greater than comparison on a date in a Rails 3 search?
我有这个助手,可以在 select 输入中显示事件模型中的月份和年份。我如何只显示当前和未来的月份而不显示过去的月份?
def select_month_tag(events)
html = <<-HTML
<select id="filtro-mes-ano" class="event-filter selectCustom2 event_filter_select">
<option value="Filtrar por mês" disabled selected >Filtrar por mês</option>
HTML
events.each do | event |
if not date_and_month(event.month_ref, event.year_ref).blank?
html += <<-HTML
<option data-year="#{event.year_ref}"
data-month="#{event.month_ref}"
"#{'selected' if is_hash_selected?(event)}">
date_and_month(event.month_ref, event.year_ref)}
</option>
HTML
end
end
html += <<-HTML
</select>
HTML
html.html_safe
end
谢谢。
我猜你会为你编写的那种代码做这样的事情。 让我知道它是否适合你
def select_month_tag(events)
html = <<-HTML
<select id="filtro-mes-ano" class="event-filter selectCustom2 event_filter_select">
<option value="Filtrar por mês" disabled selected >Filtrar por mês</option>
HTML
events.each do | event |
if not date_and_month(event.month_ref, event.year_ref).blank?
# Get today's date
today = Date.today
# check if event year is greater than present date's year
if event.year_ref >= today.year
# check if event month is greater than present date's month
if event.month_ref >= today.month # considering month_ref is a number between 1..12
html += <<-HTML
<option data-year="#{event.year_ref}"
data-month="#{event.month_ref}"
"#{'selected' if is_hash_selected?(event)}">
date_and_month(event.month_ref, event.year_ref)}
</option>
HTML
end
end
end
end
html += <<-HTML
</select>
HTML
html.html_safe
end
为什么不过滤 Event
集合,然后用它创建选项?我认为您不希望助手中有这么多逻辑。最好在模型上做一个范围,然后使用它。像
Event.where('date > ?', date_variable_here)
Can you do greater than comparison on a date in a Rails 3 search?