我正在製作一個應用程序,用戶可以預約一小時的培訓時間。我想讓用戶選擇在培訓中看到誰(小時),我在培訓中做了索引預訂,但它顯示了所有已完成書籍的用戶。我希望索引只顯示在某個小時內預訂的用戶,我的意思是如果用戶在training_id中預訂了:7.當我去http://localhost:3000/trainings/7/bookings時,我想只看到那個小時內預訂的用戶試圖創建索引用戶在每個小時內預訂
這是我的代碼:
登記控制器
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(@training) %>
</li>
<% end %>
</ul>
耙路線:
[email protected]:~/Apps/boxApp$ rake routes
Prefix Verb URI Pattern Controller#Action
root GET / static_pages#home
signup GET /signup(.:format) users#new
contact GET /contact(.:format) static_pages#contact
about GET /about(.:format) static_pages#about
login GET /login(.:format) sessions#new
POST /login(.:format) sessions#create
logout DELETE /logout(.:format) sessions#destroy
book GET /book(.:format) bookings#new
POST /book(.:format) bookings#create
unbook DELETE /unbook(.:format) bookings#destroy
edit_account_activation GET /account_activations/:id/edit(.:format) account_activations#edit
password_resets POST /password_resets(.:format) password_resets#create
new_password_reset GET /password_resets/new(.:format) password_resets#new
edit_password_reset GET /password_resets/:id/edit(.:format) password_resets#edit
password_reset PATCH /password_resets/:id(.:format) password_resets#update
PUT /password_resets/:id(.:format) password_resets#update
training_bookings GET /trainings/:training_id/bookings(.:format) bookings#index
POST /trainings/:training_id/bookings(.:format) bookings#create
new_training_booking GET /trainings/:training_id/bookings/new(.:format) bookings#new
edit_training_booking GET /trainings/:training_id/bookings/:id/edit(.:format) bookings#edit
training_booking GET /trainings/:training_id/bookings/:id(.:format) bookings#show
PATCH /trainings/:training_id/bookings/:id(.:format) bookings#update
PUT /trainings/:training_id/bookings/:id(.:format) bookings#update
DELETE /trainings/:training_id/bookings/:id(.:format) bookings#destroy
trainings GET /trainings(.:format) trainings#index
POST /trainings(.:format) trainings#create
new_training GET /trainings/new(.:format) trainings#new
edit_training GET /trainings/:id/edit(.:format) trainings#edit
training GET /trainings/:id(.:format) trainings#show
PATCH /trainings/:id(.:format) trainings#update
PUT /trainings/:id(.:format) trainings#update
DELETE /trainings/:id(.:format) trainings#destroy
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
你能告訴'耙routes'的輸出?我認爲你可以讓你的預訂#索引路線使用'@bookings = Booking.where(training_id:params [:training_id])''。 –
用耙路線編輯 –
它工作兄弟!添加它作爲答案,所以我可以接受它!上帝祝福你 –