Search code examples
ruby-on-railsruby-on-rails-3formtastic

How to get default value for Formtastic date / time select?


I am using Formtastic 1.2.3. I want the current date and time already to be selected when the form is loaded. I tried many combinations with :default or :selected, but none worked. Even from the github page of Formtastic I can't get information on that. Any suggestions? Thanks for your time!

= form.input :date,:hint => 'Select a date',
             :prompt => {:day => "Day", :month => "Month", :year => "Year"},
             :start_year => Time.now.year
= form.input :time

Solution

  • I've done this by setting the value in the active record object directly when it's created like so:

    In the model class:

    def after_initialize
      self.start_time ||= Time.now
    end
    

    Then when the form is displayed, it will already have the time populated and set correctly.