我有預訂模型,控制器,視圖。用戶可以預訂,司機稍後可以申請預訂。預訂創建工作正常,我可以向司機顯示所有無人認領的預訂,但是當司機打開索賠按鈕時,我無法使用司機ID更新預訂。我已將driver_id添加到預訂模型,並嘗試使用預訂控制器中的更新方法更新驅動程序的字段,我想我沒有正確地將參數傳遞給link_to。有人可以告訴我什麼,我做錯了:將不正確的參數傳遞給link_to
訂房控制器:
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
驅動控制器
類DriversController <的ApplicationController before_action:authenticate_driver!除了:[:秀]
def show
@driver = Driver.find(params[:id])
end
def index
@bookings = Booking.where(:driver_id => nil)
end
結束
(驅動器/ index.html.erb)駕駛員視野
<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
耙路線
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
你能分享你的路線......第二個參數不是通過的方式......但是我只能在看到路線後才能給你正確的路線...... – SnehaT
謝謝SnehaT,我已經添加了路線。請讓我知道你認爲你的想法 – Mayank