我做了这个简单的表格,我不想及时预订。有什么命令可以解决这个问题吗?
<div class="container">
<div class="row justify-content-center align-items-center">
<div class="col">
<div class="booking-card p-5 mt-5">
<h1>Create a new booking!</h1>
<%= simple_form_for [@listing, @booking] do |f| %>
<%= f.input :start_date %>
<%= f.input :end_date %>
<div class="form-actions">
<%= f.button :submit, "Create booking", class:"button-28" %>
</div>
<% end %>
</div>
</div>
</div>
</div>发布于 2022-02-17 21:11:38
我同意上面的答案,您应该添加一个模型验证,以防止数据库保存日期在过去。
我看到您正在使用SimpleForm gem,它们可能有一个与内置Rails助手类似的选项。
如果您想要添加稍微好一点的用户体验,我建议使用Datetime表单字段。Rails有很多内置的表单帮助程序,包括一个选择日期输入的工具。
使用此助手,您可以设置最小有效日期,从而防止用户在过去/“返回时间”中输入日期。
# Example with Rails FormHelper
<%= form_for [@listing, @booking] do |f| %>
<%= f.date_field :start_date, min: Date.today %>
<%= f.date_field :end_date %>
<div class="form-actions">
<%= f.submit "Create booking", class:"button-28" %>
</div>
<% end %>发布于 2022-02-17 19:05:16
您需要将验证添加到预订模型中。
https://stackoverflow.com/questions/71161764
复制相似问题