尝试在一小时培训中为预订创建索引
Trying to create an index for bookings in a hour training
我正在开发一个应用程序,用户可以在其中预订一小时的培训。我想让用户可以选择查看谁在培训中预订(小时),我正在培训中进行索引预订,这是我的代码:
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 created"
redirect_to trainings_path
else
render 'new'
end
end
def index
@bookings = Booking.all
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'
get '/book', to: 'bookings#new'
post '/book', to: 'bookings#create'
delete '/unbook', to: 'bookings#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :trainings do
resources :bookings
end
resources :users
end
当我去参加培训节目(特定时间的培训)时,代码如下:
<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>
_index_bookings.html.erb是:
<ul class="bookings">
<% if current_user.bookings(@training) %>
<li>
<%= link_to @training_id, training_bookings_path %>
</li>
<% end %>
</ul>
应用程序给我错误:
Showing
/home/cesar/Apps/boxApp/app/views/bookings/_index_bookings.html.erb
where line #4 raised:
No route matches {:action=>"index", :controller=>"bookings", :id=>"7"}
missing required keys: [:training_id]
我想知道为什么它不采用 training_id,如果它采用 class 的 ID,即 7。以及如何修复它。
使用嵌套资源 url 时,您应该将父资源作为第一个参数传递,如下所示:
training_bookings_path(@training)
我正在开发一个应用程序,用户可以在其中预订一小时的培训。我想让用户可以选择查看谁在培训中预订(小时),我正在培训中进行索引预订,这是我的代码:
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 created"
redirect_to trainings_path
else
render 'new'
end
end
def index
@bookings = Booking.all
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'
get '/book', to: 'bookings#new'
post '/book', to: 'bookings#create'
delete '/unbook', to: 'bookings#destroy'
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
resources :trainings do
resources :bookings
end
resources :users
end
当我去参加培训节目(特定时间的培训)时,代码如下:
<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>
_index_bookings.html.erb是:
<ul class="bookings">
<% if current_user.bookings(@training) %>
<li>
<%= link_to @training_id, training_bookings_path %>
</li>
<% end %>
</ul>
应用程序给我错误:
Showing /home/cesar/Apps/boxApp/app/views/bookings/_index_bookings.html.erb where line #4 raised:
No route matches {:action=>"index", :controller=>"bookings", :id=>"7"} missing required keys: [:training_id]
我想知道为什么它不采用 training_id,如果它采用 class 的 ID,即 7。以及如何修复它。
使用嵌套资源 url 时,您应该将父资源作为第一个参数传递,如下所示:
training_bookings_path(@training)