Rails 在视图中的下拉列表中格式化日期 [英] Rails Format Date in Drop Down List in View

查看:13
本文介绍了Rails 在视图中的下拉列表中格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以帮助您在视图中的下拉列表中格式化日期字段.我的日期显示带有时间戳,我希望它们显示为mm/dd/yyyy".

I could you some assistance formatting a date field in a drop-down list in a for in a view. My dates are showing with a time-stamp and I'd like them to show as "mm/dd/yyyy".

在我的模型中,我有一个从表中选择日期字段的方法:

In my model I have a method selecting a date field from the table:

def self.get_event_dates
  event_dates = UgradRsvp.find_by_sql("select distinct event_date from admissions.ugrad_rsvps where event_date is not null order by event_date desc")
end 

我们使用 Oracle 作为我们的数据库,所以我也尝试使用 to_char(event_date, 'MM/DD/YYYY') 作为 event_date,但是我的下拉列表中的日期仍然带有时间戳.

We use Oracle as our db, so I also tried using to_char(event_date, 'MM/DD/YYYY') as event_date, but the dates in my drop-down still show with a time-stamp.

我的视图是一个搜索表单.用户将选择一个日期,然后查看在该 event_date 报名参加活动的所有学生的报告:

My view is a search form. Users will select a date and then see a report of all students signed up for an event on that event_date:

<%= select_tag "event_date", options_from_collection_for_select(UgradRsvp.get_event_dates, "event_date", "event_date") %>

我的表 ugrad_rsvps 中有大约八个日期.它们显示为,即:2013-04-18 00:00:00 -0400"我想让它们显示为04/18/2013"​​.我尝试使用 strftime("%m/%d/%Y"),但在我的视图中的下拉菜单(上图)中出现错误.

There are about eight dates in my table ugrad_rsvps. They are showing as, i.e: "2013-04-18 00:00:00 -0400" I'd like to have them show as "04/18/2013". I've tried using strftime("%m/%d/%Y"), but I just get errors on the line in my view for the drop-down (above).

有什么想法吗?谢谢,

推荐答案

在你的模型中添加以下方法

In your model add the following method

  def event_date_formatted
    event_date.strftime("%m/%d/%Y")
  end

那么你可以像这样简单地使用:

Then you can simply use like so:

<%= select_tag "event_date", options_from_collection_for_select(UgradRsvp.get_event_dates, 'event_date', :event_date_formatted) %>

这篇关于Rails 在视图中的下拉列表中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆