我正在创建一个用户可以预订培训的应用程序。我希望这个应用程序让用户每天预订一次,而不是更多。
我正在尝试在User模型中创建一个名为not_booked?
的方法,如果参数为true,则让用户预订,否则取消预订。
这在我的应用程序中不起作用,我想知道原因,以及如何解决它。
用户模型:
class User < ApplicationRecord
has_many :trainings, through: :bookings
has_many :bookings
def not_booked?(booked)
booked = self.bookings(@training)
booked = true
end
.
.
.
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.html.erb视图是:
<% unless current_user?(@user) %>
<% if current_user.not_booked?(@training) %>
<%= link_to "Book", new_training_booking_path(@training), class: "btn btn-primary" %>
<% else %>
<% @bookings.each do |booking| %>
<%= link_to "Unbook", training_booking_path(booking.training, booking), method: :delete, data: { confirm: 'Are you certain you want to delete this?' }, class: "btn btn-primary" %>
<% end %>
<% end %>
<% end %>
当我保留时,如果我再次尝试再做,我可以,但我不希望用户做多本书。
也许我不知道如何为用户将预订状态设置为true。我想知道我该怎么做。
答案 0 :(得分:1)
您的not_booked?
方法应该如下所示:
def not_booked?(training)
bookings.where(training_id: training.id).none?
end
将其视为
如果
,则用户t
的所有预订中u
都没有u
b
不会预订培训b.training_id == t.id