ruby-on-railsruby-on-rails-4

How to get the value of date_select field in Rails?


I need to get the value of the date_select field in my controller. I also need to add an if statement so that if the value is nil it won't try to get the data and trow an error.

Here's my date_select field :

<%= date_select :regdate, :date, order: [:year, :month] %>

Here's the params in the debug :

regdate: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  date(3i): '1'
  date(1i): '2014'
  date(2i): '10' 

Solution

  • In the controller side

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

    or this, whatever coming in your params

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