noob-on-rails
noob-on-rails

Reputation: 35

Form select 15 minutes from current time

So I currently have a car booking functionality.

At the moment a booking start_time is set to "DateTime.now + 15.minutes" so when a booking is created the start time is automatically 15 minutes from the current time .

The user is able to pick the end_time of the booking using Time_select like this:

 <div class="field">
    <text><label><%= form.label :end_time %></label></text>
    <div class="col-md-0">
      <%= form.time_select :end_time, id: :booking_end_time %>
    </div>
 </div>

What I am trying to achieve is instead of having the user pick a time by individually picking the hour and minute values is to have something like this:

<select name="post[end_time]" >
  <option value= "#{DateTime.now + 30.minutes}" >15 Minutes</option>
  <option value= "#{DateTime.now + 45.minutes}" >30 Minutes</option>
  <option value= "#{DateTime.now + 60.minutes}" >45 Minutes</option>
</select> 

To have them pick from a dropdown menu where they can pick options like "15 minutes" which means they will be booking the car for 15 minutes

For more clarity this is what part of my booking create function looks like at the moment

def create
    params[:booking][:user_id]= current_user.id
    params[:booking][:start_time]= DateTime.now + 15.minutes
    @booking = Booking.new(booking_params)

Any tips on how to go about my problem would be greatly appreciated

Upvotes: 0

Views: 544

Answers (1)

Ravi Teja Dandu
Ravi Teja Dandu

Reputation: 476

Instead of directly setting it as <option value= "#{DateTime.now + 30.minutes}" >15 Minutes</option> you can set the options as follows:

<select name="post[end_time]" >
  <option value= "30" >15 Minutes</option>
  <option value= "45" >30 Minutes</option>
  <option value= "60" >45 Minutes</option>
</select>

Now you can modify your controller create as follows:

def create
  params[:booking][:user_id]= current_user.id
  params[:booking][:start_time]= DateTime.now + 15.minutes
  params[:booking][:end_time]= DateTime.now + (params[:post][:end_time].to_i).minutes
  @booking = Booking.new(booking_params)
end

Hope this is what you are looking for.

Upvotes: 1

Related Questions