How to get a Date from date_select or select_date in Rails?

后端 未结 7 1185
小鲜肉
小鲜肉 2020-11-30 04:05

Using select_date gives me back a params[:my_date] with year, month and day attributes. How do get a Date ob

相关标签:
7条回答
  • 2020-11-30 04:27

    I use the following method, which has the following benefits:

    1. it doesn't have to explicitly name param keys xxx(1i) through xxx(3i) (and thus could be modified to capture hour and minute simply by changing Date to DateTime); and
    2. it extracts a date from a set of params even when those params are populated with many other key-value pairs.

    params is a hash of the format { xxx(1i): '2017', xxx(2i): 12, xxx(3i): 31, ... }; date_key is the common substring xxx of the target date parameters.

    def date_from_params(params, date_key)
      date_keys = params.keys.select { |k| k.to_s.match?(date_key.to_s) }.sort
      date_array = params.values_at(*date_keys).map(&:to_i)
      Date.civil(*date_array)
    end
    

    I chose to place this as a class method of ApplicationRecord, rather than as an instance helper method of ApplicationController. My reasoning is that similar logic exists within the ActiveRecord instantiator (i.e., Model.new) to parse dates passed in from Rails forms.

    0 讨论(0)
  • 2020-11-30 04:31

    With the date_select example @joofsh's answer, here's a "one liner" I use, presuming the date field is called start_date:

    ev_params = params[:event]
    
    date = Time.zone.local(*ev_params.select {|k,v| k.to_s.index('start_date(') == 0 }.sort.map {|p| p[1].to_i})
    
    0 讨论(0)
  • 2020-11-30 04:32

    Using date_select gives you 3 separate key/value pairs for the day, month, and year respectively. So you can pass them into Date.new as parameters to create a new Date object.

    An example date_select returned params for an Event model:

    "event"=>
     {"name"=>"Birthday",
      "date(1i)"=>"2012",
      "date(2i)"=>"11",
      "date(3i)"=>"28"},
    

    Then to create the new Date object:

    event = params[:event]
    date = Date.new event["date(1i)"].to_i, event["date(2i)"].to_i, event["date(3i)"].to_i
    

    You may instead decide to wrap this logic in a method:

    def flatten_date_array hash
      %w(1 2 3).map { |e| hash["date(#{e}i)"].to_i }
    end
    

    And then call it as date = Date.new *flatten_date_array params[:event]. But this is not logic that truly belongs in a controller, so you may decide to move it elsewhere. You could even extend this onto the Date class, and call it as date = Date.new_from_hash params[:event].

    0 讨论(0)
  • 2020-11-30 04:32

    Here is another one:

    # view
    <%= date_select('event', 'date') %>
    
    # controller
    date = Date.civil(*params[:event].sort.map(&:last).map(&:to_i))
    

    Found at http://kevinlochner.com/use-rails-dateselect-without-an-activerecord

    0 讨论(0)
  • 2020-11-30 04:35

    Or simply do this:

    your_date_var = Time.parse(params[:my_date])
    
    0 讨论(0)
  • 2020-11-30 04:40

    Here is the another one

    Date.civil(params[:event]["date(1i)"].to_i,params[:event]["date(2i)"].to_i,params[:event]["date(3i)"].to_i)
    
    0 讨论(0)
提交回复
热议问题