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

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

问题描述

我可以帮助您格式化视图中for下拉列表中的日期字段.我的日期显示有时间戳,我希望它们显示为"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).

有什么想法吗?谢谢,

推荐答案

在模型中添加以下方法

  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天全站免登陆