Ruby on rails 培训满时不显示问题
我正在创建一个应用程序,用户可以在其中预订培训。我说过,当训练达到最大时,没有人可以再预订了。我试图在出现这种情况时显示一个错误,但错误没有出现,方法有效,它不允许任何人加入培训,但应用程序需要告诉用户,他无法预订,因为培训已满 我想要一些帮助,以了解为什么错误没有显示,以及如何修复它 预订模式:Ruby on rails 培训满时不显示问题,ruby-on-rails,ruby,model-view-controller,Ruby On Rails,Ruby,Model View Controller,我正在创建一个应用程序,用户可以在其中预订培训。我说过,当训练达到最大时,没有人可以再预订了。我试图在出现这种情况时显示一个错误,但错误没有出现,方法有效,它不允许任何人加入培训,但应用程序需要告诉用户,他无法预订,因为培训已满 我想要一些帮助,以了解为什么错误没有显示,以及如何修复它 预订模式: class Booking < ApplicationRecord belongs_to :user belongs_to :training validates :user_id,
class Booking < ApplicationRecord
belongs_to :user
belongs_to :training
validates :user_id, presence: true
validates :training_id, presence: true
validate :training_not_full?, on: :create
private
def training_not_full?
errors.add(:training, "Full training. Try another hour") unless training.can_book?
end
end
class Training < ApplicationRecord
has_many :users, through: :bookings
has_many :bookings
def can_book?
bookings.count < slots
end
end
培训模式:
class Booking < ApplicationRecord
belongs_to :user
belongs_to :training
validates :user_id, presence: true
validates :training_id, presence: true
validate :training_not_full?, on: :create
private
def training_not_full?
errors.add(:training, "Full training. Try another hour") unless training.can_book?
end
end
class Training < ApplicationRecord
has_many :users, through: :bookings
has_many :bookings
def can_book?
bookings.count < slots
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>
预订表格视图:
<%= form_for(@training) do |f| %>
<% if f.object.errors.any? %>
<% f.object.errors.full_messages.each do |full_message| %>
<p>
<%= full_message %>
</p>
<% end %>
<% end %>
<% 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 %>
<% end %>
新预订视图:
<h1>Book confirmation</h1>
<div class="row">
<div class="col-md-6 col-md-offset-3">
<%= form_for ([@training, @booking]) do |f| %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :training_id, value: "" %>
<%= f.submit "Confirm booking", class: "btn btn-primary" %>
<% end %>
看起来add方法的实现在Rails 5中有所改变 请试一试
def training_not_full?
errors.add(:training, :full, message: "Full training. Try another hour") unless training.can_book?
end
而不是
def training_not_full?
errors.add(:training, "Full training. Try another hour") unless training.can_book?
end
请更改新的.html.erb表单:
<%= form_for ([@training, @booking]) do |f| %>
<% if f.object.errors.any? %>
<% f.object.errors.full_messages.each do |full_message| %>
<p>
<%= full_message %>
</p>
<% end %>
<% end %>
<%= f.hidden_field :user_id, value: current_user.id %>
<%= f.hidden_field :training_id, value: "" %>
<%= f.submit "Confirmar reservacion", class: "btn btn-primary" %>
<% end %>
它现在向我显示了这个错误:未定义的方法'except'用于完整训练。再试试一个小时:你是什么意思?执行官。请求参数:{utf8=>✓, 真实性令牌=>sCV7T3akprwsTvH1FO/X8powg7xFzO9yS/D7PLGSNCSEZQFOCCMXTRMF5KXXQFQEGYKJDUM1QRDOJWDM7ZKNRUQ==,预订=>{user\u id=>2,training\u id=>},commit=>Confirmar reservacion,training\u id=>1}请查看我修改后的答案,我不得不添加消息:现在它没有给我任何错误,但仍然没有显示与以前一样的消息。没有像flash这样的方法可以让我显示消息?你能分享你的视图文件吗?你可以随时使用rails脚手架作为参考。