2016-08-04 46 views
0

我有预订模型,控制器,视图。用户可以预订,司机稍后可以申请预订。预订创建工作正常,我可以向司机显示所有无人认领的预订,但是当司机打开索赔按钮时,我无法使用司机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 
+0

你能分享你的路线......第二个参数不是通过的方式......但是我只能在看到路线后才能给你正确的路线...... – SnehaT

+0

谢谢SnehaT,我已经添加了路线。请让我知道你认为你的想法 – Mayank

回答

1

试试这个:

<%= link_to "Claim", booking_path(booking.id, driver_id: current_driver.id), :method => :patch, class: "btn btn-primary" %> 
+0

也尝试过,因为#<#:0x007f8be011a0a8> – Mayank

+0

通过执行'rake routes'命令来检查路由,我得到一个错误未定义的方法'update_bookings_path'。 –

+0

我的路线绝对显示更新方法 - 将耙路线添加到主要描述 – Mayank