【发布时间】:2017-03-23 22:05:33
【问题描述】:
我创建了一个应用程序,用户可以在其中预订一小时的培训。该应用程序已经完成,但是用户可以同时预订多个培训小时的错误。
如果用户已经预订,我想这样做,预订按钮会在其他培训中消失(如果他参加了他已经预订的培训,取消预订按钮将出现)。我想寻求帮助来完成这项工作。我的代码如下:
用户模型:
class User < ApplicationRecord
has_many :trainings, through: :bookings
has_many :bookings
def not_booked?(training)
bookings.where(training: training).none?
end
显示训练视图:
<div class="row">
<section>
<h1>
HOUR: <%= @training.hour %>
</h1>
</section>
<section>
<h1>
SLOTS: <%= @training.left_slots %>
</h1>
</section>
<center>
<%= render 'bookings/booking_form' if logged_in? %>
<%= render 'bookings/index_bookings' if logged_in? %>
</center>
booking_form 视图:
<% unless current_user?(@user) %>
<% if current_user.not_booked?(@training) %>
<%= link_to "Book", new_training_booking_path(@training),
class: "btn btn-primary" %>
<% else %>
<%= link_to "Unbook",
training_booking_path(@training, @training.bookings.where(user: current_user).first),
method: :delete, data:{ confirm: 'Sure?' },
class: "btn btn-primary" %>
<% end %>
<% end %>
为了记录,预订在每天午夜被删除,所以我只需要正确的代码让用户只预订一次(如果他被预订,不要让他在另一个小时内预订)
谢谢
【问题讨论】:
标签: ruby-on-rails ruby model-view-controller