我正在制作一个用户可以预订一小时培训的应用程序。我想让用户可以选择在培训中预订谁(小时),我在培训中进行了索引预订,但它显示了所有已经完成预订的用户。我希望索引只显示在A CERTAIN小时预订的用户,我的意思是如果用户在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>
耙路线:
cesar@Dandy:~/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
答案 0 :(得分:0)
当你在路线中写下这个:
resources :trainings do
resources :bookings
end
您可以在rake routes
中看到结果:
training_bookings GET /trainings/:training_id/bookings(.:format)
这会被路由到Trainings::BookingsController#index
(您不必明确声明此映射;它会自动与resources
一起发生)。
您可以在rake routes
定义中看到一个路径参数(尽管如此,用户可以在问号后面的查询参数中发送他们想要的任何内容)。您需要使用此training_id
来过滤索引路由返回的预订;这并不是因为您嵌套路线而自动发生的。
这就是你如何做到的:
def index
@bookings = Booking.where(training_id: params[:training_id])
end