Rails Format Date in Drop Down List in View

后端 未结 2 671
臣服心动
臣服心动 2021-01-14 22:56

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/yyy

相关标签:
2条回答
  • 2021-01-14 23:33

    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) %>
    
    0 讨论(0)
  • 2021-01-14 23:48

    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") %>
    
    0 讨论(0)
提交回复
热议问题