Search code examples
ruby-on-railsrubymodel-view-controllerrestful-architecture

Trying to create an index for bookings in a hour training


Im making an application where an user can book a hour of training. I want to give the user the option to see who is booked in the training (hour), i'm making the index bookings in a training, this is my code:

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

Booking model:

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

My 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

When i go to training show (a specific hour of training) the code is the following:

<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>

The _index_bookings.html.erb is:

<ul class="bookings">
<% if current_user.bookings(@training) %>
  <li>
<%= link_to @training_id, training_bookings_path %>
</li>
<% end %>
</ul>

The app gives me the error:

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]

I would like to know why it does not take the training_id, if it is taking the id of the class which is 7. And how to fix it.


Solution

  • When using nested resource urls, you should pass the parent resource as the first parameter, like so:

    training_bookings_path(@training)