0

What would be the best way to combine two fields in a form into a single model field in Rails 4? My form has separate date and time picker text inputs, and I'd like those to be combined on the fly into a single timestamp field.

<%= f.text_field :published_at, class: 'datepicker' %>
<%= f.text_field :published_at, class: 'timepicker' %>
Jonah
  • 9,991
  • 5
  • 45
  • 79

2 Answers2

2

One common option to approach a form with logic is to use the form object pattern.

http://pivotallabs.com/form-backing-objects-for-fun-and-profit/

http://railscasts.com/episodes/416-form-objects (pro)

Another common way to handle this would be to make the individual components of the attribute with attr_accessor and combine them into the "real" attribute in a before_save in the model.

Brad Werth
  • 17,411
  • 10
  • 63
  • 88
0

User @Chris Barretto makes a wonderful job explaining how you could do this: https://stackoverflow.com/a/6730104/2620080

You can try to change the input to data_select and time_select.

Community
  • 1
  • 1
ScieCode
  • 1,706
  • 14
  • 23