1
votes

When user clicks AddDate in my event planning app, I add a row as seen below so user can add a date and start/end time, and AM/PM.

But in my form using nested attributes, it seems my select controls need a reference to the model field they set (:start_time and :end_time). But the start and end times are create from TWO select controls, one for the hours and one for the minutes. So I'm not sure how the values chosen in the two selects will be combined to form the start and end times.

screenshot

<div class="user_event_inline_container margin_left_ten padding_right_gone">
  <%= f.label  :start_time, "Start", class: 'info_inline_control info_label five_margin_right' %>
  <%= f.select :start_time, options_for_select([['1',1],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10],['11',11],['12',12]]), class: (field_class(@user_event, :start_time) + 'info_inline_control') %>
  <%= f.select :start_time, options_for_select([['00',1],['15',2],['30',3],['45',4]]), class: (field_class(@user_event, :start_time) + 'info_inline_control') %>
  <%= f.select :start_am_pm, options_for_select([['am',1],['pm',2]]), class: (field_class(@user_event, :start_am_pm) + 'info_inline_control') %>
</div>

<div class="user_event_inline_container margin_left_ten padding_right_gone">
  <%= f.label  :end_time, "End", class: 'info_inline_control info_label five_margin_right' %>
  <%= f.select :end_time, options_for_select([['1',1],['2',2],['3',3],['4',4],['5',5],['6',6],['7',7],['8',8],['9',9],['10',10],['11',11],['12',12]]), class: (field_class(@user_event, :end_time) + 'info_inline_control') %>

  <%= f.select :end_time, options_for_select([['00',1],['15',2],['30',3],['45',4]]), class: (field_class(@user_event, :end_time) + 'info_inline_control') %>
  <%= f.select :end_am_pm, options_for_select([['am',1],['pm',2]]), class: (field_class(@user_event, :end_am_pm) + 'info_inline_control') %>
</div>
2
Can you edit your question and include the POST variables your browser submits with the form? One possibility would be to name your select fields f.select :start_hour f.select :start_minute and then in your controller concatenate those string values and assign them to :start_time. Currently I believe start_time is being overwritten each time you add a select named start_time.ctilley79
You were right, overwriting attributes. I added this to the EventDate model - attr_accessible :start_hour, :start_minute, :end_hour, :end_minute but I get this error - undefined method `start_hour' for #<EventDate:0x50f33e8>Greg Lafrance
Also not sure what to put in the controller. This is what I have for the UserEvent controller create method: def create @user_event = current_user.user_events.build(params[:user_event])Greg Lafrance
Added these methods in the EventDate model, but now I'm getting "unknown attribute: start_hour" error in UserEvent controller in create action: def start_hour :start_hour end def start_minute :start_minute end def end_hour :end_hour end def end_minute :end_minute end def start_time "#{start_hour}:#{start_minute}:00" end def end_time "#{end_hour}:#{end_minute}:00" endGreg Lafrance
I now have this in create action, but now am getting this error "syntax error, unexpected '\n', expecting tCOLON2 or '[' or '.'" - for user_event.event_dates.each do |date_item| date_item.start_time = "#{date_item.start_hour}:#{date_item.start_minute}:00" date_item.end_time = "#{date_item.end_hour}:#{date_item.end_minute}:00" end @user_event = current_user.user_events.build(params[:user_event])Greg Lafrance

2 Answers

1
votes

Why don't you use a time_select instead?

0
votes

Doing it this way, you'll need to merge them manually in your controller using params[] values and Date.parse().

You can also try using f.time_select, which should function as a normal field after you submit.

Might find this useful: Where is the Rails method that converts data from `datetime_select` into a DateTime object?

Personally, I write custom interfaces for my date fields using jQuery plugins and then use javascript to merge all the fields into a hidden field that actually gets submitted.