2

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 

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.

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") %>

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).

Any ideas? Thanks,

4

2 回答 2

4

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) %>
于 2013-03-30T17:35:06.620 回答
0

I not try yet, but you can try to add method to Model like

class UgradRsvp < ActiveRecord::Base
    def event_date_text
        event_date.strftime("%d/%m/%Y")
    end
end

and then call event_date_text as txt_method

<%= select_tag "event_date", options_from_collection_for_select(UgradRsvp.get_event_dates, "event_date", "event_date_text") %>
于 2013-03-30T18:08:16.707 回答