Home > Net >  Stoping rails from going back in time when booking?
Stoping rails from going back in time when booking?

Time:02-18

I made this simple form and I don't want to book back in time. Is there any command which fixes the problem?

<div >
      <div >
        <div >
          <div >
            <h1>Create a new booking!</h1>
              <%= simple_form_for [@listing, @booking] do |f| %>
                <%= f.input :start_date %>
                <%= f.input :end_date %>
                <div >
                  <%= f.button :submit, "Create booking", class:"button-28" %>
                </div>
              <% end %>
          </div>
        </div>
      </div>
    </div>

CodePudding user response:

I agree with the above answer that you should add a Model validation to prevent the database from saving dates in the past.

I see you are using the SimpleForm gem, and they probably have a similar option to the built-in Rails helper.

If you want to add a slightly better user experience, I recommend using a Datetime form field. Rails has lots of built-in Form Helpers, including one to select a Date input.

With this helper, you can set the minimum valid date, so you can prevent users from inputting dates in the past/"back in time".

# Example with Rails FormHelper

<%= form_for [@listing, @booking] do |f| %>
     <%= f.date_field :start_date, min: Date.today  %>
     <%= f.date_field :end_date %>
     <div >
         <%= f.submit "Create booking", class:"button-28" %>
     </div>
<% end %>

CodePudding user response:

You need to add validation to the booking model.

  • Related