I have a bookings model, controller, view. Users can make a booking and a driver can later claim a booking. The booking creation is working fine, I am able to display all the unclaimed bookings to a driver, however I am unable to update the booking with a Driver ID when the driver hits the claim button. I have added the driver_id to the booking model and trying to use the update method in bookings controller to update the driver's field, I think I am not passing the parameters correctly to the link_to. Can someone please tell me what I am doing wrong:
Booking Controller:
def update
if @booking.update(booking_params)
redirect_to @booking, notice: "Updated..."
else
render :edit
end
end
private
def set_booking
@booking = Booking.find(params[:id])
end
def booking_params
params.require(:booking).permit(:location_pickup, :location_dropoff, :date_pickup, :date_dropoff, :weight, :load_type)
end
end
Driver controller
class DriversController < ApplicationController before_action :authenticate_driver!, except: [:show]
def show
@driver = Driver.find(params[:id])
end
def index
@bookings = Booking.where(:driver_id => nil)
end
end
(drivers/index.html.erb) Driver View
<div class="row">
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">
Listings
</div>
<div class="panel-body">
<%= current_driver.email %>
<% @bookings.each do |booking| %>
<div class="row">
<div class="col-md-2">
</div>
<div class="col-md-7">
<td> <%= booking.location_pickup %> </td>
<td><%= booking.location_dropoff %></td>
</div>
<div class="col-md-7">
<%= link_to "Claim", update_bookings_path(@booking, driver_id: current_driver.id), :method => :patch, class: "btn btn-primary" %>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>
routes.rb
Rails.application.routes.draw do
root 'pages#home'
devise_for :users,
:path => '',
:path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile'},
:controllers => {:registrations => 'registrations'
}
devise_for :drivers,
:path => '/drivers',
:path_names => {:sign_in => 'login', :sign_out => 'logout', :edit => 'profile'},
:controllers => { :registrations => "drivers/registrations" }
resources :users, only: [:show]
resources :drivers, only: [:show, :index, :claim]
resources :bookings
end
Rake routes
edit_booking GET /bookings/:id/edit(.:format) bookings#edit
booking GET /bookings/:id(.:format) bookings#show
PATCH /bookings/:id(.:format) bookings#update
PUT /bookings/:id(.:format) bookings#update
DELETE /bookings/:id(.:format) bookings#destroy
Try this:
<%= link_to "Claim", booking_path(booking.id, driver_id: current_driver.id), :method => :patch, class: "btn btn-primary" %>