将不正确的参数传递给 link_to
Passing incorrect parameters to link_to
我有一个预订模型、控制器、视图。用户可以进行预订,driver 稍后可以申请预订。预订创建工作正常,我可以将所有无人认领的预订显示到 driver,但是当 driver 提出索赔时,我无法使用 Driver ID 更新预订按钮。我已将 driver_id 添加到预订模型并尝试使用预订控制器中的更新方法来更新 driver 的字段,我想我没有将参数正确传递给 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
Driver 控制器
class DriversController < ApplicationController
before_action :authenticate_driver!, 除了: [:show]
def show
@driver = Driver.find(params[:id])
end
def index
@bookings = Booking.where(:driver_id => nil)
end
结束
(drivers/index.html.erb) Driver 查看
<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
试试这个:
<%= link_to "Claim", booking_path(booking.id, driver_id: current_driver.id), :method => :patch, class: "btn btn-primary" %>
我有一个预订模型、控制器、视图。用户可以进行预订,driver 稍后可以申请预订。预订创建工作正常,我可以将所有无人认领的预订显示到 driver,但是当 driver 提出索赔时,我无法使用 Driver ID 更新预订按钮。我已将 driver_id 添加到预订模型并尝试使用预订控制器中的更新方法来更新 driver 的字段,我想我没有将参数正确传递给 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
Driver 控制器
class DriversController < ApplicationController before_action :authenticate_driver!, 除了: [:show]
def show
@driver = Driver.find(params[:id])
end
def index
@bookings = Booking.where(:driver_id => nil)
end
结束
(drivers/index.html.erb) Driver 查看
<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
试试这个:
<%= link_to "Claim", booking_path(booking.id, driver_id: current_driver.id), :method => :patch, class: "btn btn-primary" %>