我正在制作一个用户可以预订一小时培训的应用程序。我想给应用程序一个限制,当用户已经预订了一小时的培训时,它无法预订更多的培训,至少他会删除它或书籍到期。
我的代码是:
预订控制器:
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 %>
我收到以下错误:
BookingsController#destroy中的ActiveRecord :: RecordNotFound
找不到'id'= 1
的预订参数:
{ “_方法”=&gt; “中删除”, “authenticity_token”=&gt; “中0uUXRwZdbhaKl16QxDi1HCM4H8IwEvGuoFOoxmkHhowoAUgZnlWPybck9DEbCKHh42SqXs3vtc01IRTqbx05wA ==”, “training_id”=&gt;“1”,“id”=&gt;“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&lt; ApplicationController中
def show
@training = Training.find(params[:id])
end
def index
@trainings = Training.all
end
end
由于
答案 0 :(得分:2)
在这一行:
<%= 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并为每个链接包含一个单独的删除链接:
<% @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 %>