How to combine two fields in a form into one field in a model

I have a startdatetime column and want to display this as a text field (for adding datepicker) and time_select. I tried this with different methods and needed to use fields_for / simple_fields_for.

<%= f.simple_fields_for :start do |start| %>
  <%= start.input :date, :as => :string, :input_html => { :class => 'datepicker'} %>
  <%= start.input :time, :as => :time, :minute_step => 5, :input_html => { :class => 'input-small'}  %>
<% end %>

How can I convert these two fields to the desired datetime column in the model? Or is there a better way to display two fields in a form corresponding to one field in the database?

+3
source share
2 answers

You will need to parse the individual parameters in the controller and combine them to create the date and time.

I would suggest creating a model called StartTime to analyze it.

... application / models / start_time.rb

class StartTime
  extend ActiveModel::Naming

  attr_accessor :date, :time

  def initialize(date, time)
    @date = date.is_a?(String) ? Date.parse(date) : date
    @time = time
  end

  def to_timestamp
    # parse @date and @time into Datetime

end

Chronic - /

,

+4

All Articles