让用户只预订一次培训而不是更多

Making the user just book one training not more

我正在制作一个应用程序,用户可以在其中预订一小时的培训。我想给app一个限制,当一个用户已经预定了一个小时的培训,它不能再预定更多的培训,至少他删除它或者这本书过期。

我的代码是:

预订管理员:

Class BookingsController < ApplicationController
  before_action :load_training,  only: [:create]

  def new
    @booking = Booking.new
    @training = Training.find(params[:training_id])
    @booking.training_id
  end

  def create
    @booking = @training.bookings.build(booking_params)
    @booking.user = current_user

    if @booking.save
      flash[:success] = "Book done"
      redirect_to trainings_path
    else
      render 'new'
    end
  end


  def index
    @bookings = Booking.where(training_id: params[:training_id])
  end


  def destroy
    @booking = Booking.find(params[:id])
    @booking.destroy
    flash[:success] = "Book deleted"
    redirect_to trainings_path
  end



private
  def booking_params
    params.require(:booking).permit(:user_id, :training_id)
  end

  def load_training
    @training = Training.find(params[:training_id])
  end

end

预订模式:

class Booking < ApplicationRecord
  belongs_to :user
  belongs_to :training
  default_scope -> { order(created_at: :desc) }
  validates :user_id, presence: true
  validates :training_id, presence: true



end

我的routes.rb:

Rails.application.routes.draw do

  root 'static_pages#home'
  get    '/signup',               to: 'users#new'
  get    '/contact',              to: 'static_pages#contact'
  get    '/about',                to: 'static_pages#about'
  get    '/login',                to: 'sessions#new'
  post   '/login',                to: 'sessions#create'
  delete '/logout',               to: 'sessions#destroy'


  resources :account_activations, only: [:edit]
  resources :password_resets,     only: [:new, :create, :edit, :update]

  resources :trainings do
    resources :bookings
  end
  resources :users
end

训练视图索引:

<h1>Hours</h1>

<ul class="trainings">
  <% @trainings.each do |training| %>
  <li>
    <%= link_to training.hour, training_path(training) %>
  </li>
  <% end %>
</ul>

训练视图显示:

<div class="row">
    <section>
      <h1>
HOUR: <%= @training.hour %>
      </h1>
    </section>
    <section>
      <h1>
SLOTS: <%= @training.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.is_booked(@training) %>
      <%= link_to "Delete book", training_booking_path(@training), method: "delete", data: { confirm: 'Are you certain you want to delete this?' }, class: "btn btn-primary" %>
      <% else %>
      <%= link_to "Book", new_training_booking_path(@training), class: "btn btn-primary" %>
    <% end %>
<% end %>

我收到以下错误:

ActiveRecord::RecordNotFound in BookingsController#destroy

Couldn't find Booking with 'id'=1

Parameters:

{"_method"=>"delete", "authenticity_token"=>"0uUXRwZdbhaKl16QxDi1HCM4H8IwEvGuoFOoxmkHhowoAUgZnlWPybck9DEbCKHh42SqXs3vtc01IRTqbx05wA==", "training_id"=>"1", "id"=>"1"}

我想知道为什么方法没有得到booking_id

当我尝试查看训练时间时:

Started GET "/trainings/1" for 127.0.0.1 at 2017-03-11 01:03:23 -0400
Processing by TrainingsController#show as HTML
  Parameters: {"id"=>"1"}
  Training Load (0.1ms)  SELECT  "trainings".* FROM "trainings" WHERE "trainings"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Rendering trainings/show.html.erb within layouts/application
  User Load (0.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  Rendered bookings/_booking_form.html.erb (2.3ms)
  Rendered trainings/show.html.erb within layouts/application (4.0ms)
Completed 500 Internal Server Error in 6ms (ActiveRecord: 0.2ms)



ActionView::Template::Error (No route matches {:action=>"show", :controller=>"bookings", :id=>nil, :training_id=>"1"} missing required keys: [:id]):
    2:       <% if current_user.not_booked(@training) %>
    3:       <%= link_to "Reservar", new_training_booking_path(@training), class: "btn btn-primary" %>
    4:       <% else %>
    5:       <%= link_to "Eliminar reserva", training_booking_path(@training, @booking), method: :delete,
    6:        data: { confirm: 'Are you certain you want to delete this?' }, class: "btn btn-primary" %>
    7:     <% end %>
    8: <% end %>

训练模型:

class Training < ApplicationRecord
  has_many :users, through: :bookings
  has_many :bookings

  def can_book?
    bookings.count < cantidad
  end

  end

训练控制器:

class TrainingsController < ApplicationController

 def show
    @training = Training.find(params[:id])
  end

  def index
    @trainings = Training.all
  end
end

谢谢

这一行:

<%= link_to "Delete book", training_booking_path(@training), method: "delete", data: { confirm: 'Are you certain you want to delete this?' }, class: "btn btn-primary" %>

我认为路径 training_booking_path(@training) 也应该包含 @booking 实例变量,因为嵌套路由。

所以应该是training_booking_path(@training, @booking), method: :delete,等等

您必须在显示表单的视图中@booking可用

我会在你的控制台中使用 rake routes 来确认正确的路径以及需要传入哪些资源 ID

编辑:

您的培训控制器显示操作需要将预订作为实例变量提供:

def show
  @training = Training.find(params[:id])
  @bookings = @training.bookings
end

然后在表单所在的培训节目视图中,您需要遍历 @bookings 并为每个包含一个单独的删除 link:

<% @bookings.each do |booking| %>
  <%= link_to "Delete book", training_booking_path(booking.training, booking), method: :delete, data: { confirm: 'Are you certain you want to delete this?' }, class: "btn btn-primary" %>
<% end %>