将不正确的参数传递给link_to

时间:2016-08-04 11:33:28

标签: ruby-on-rails link-to

我有预订模型,控制器,视图。用户可以预订,司机可以在以后申请预订。预订创建工作正常,我能够向驾驶员显示所有无人认领的预订,但是当驾驶员点击索赔按钮时,我无法使用驾驶员ID更新预订。我已将driver_id添加到预订模型并尝试使用预订控制器中的更新方法来更新驱动程序的字段,我想我没有正确地将参数传递给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 !,除了:[:show]

def show
    @driver = Driver.find(params[:id])
end
def index
    @bookings = Booking.where(:driver_id => nil)
end

(drivers / 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

1 个答案:

答案 0 :(得分:1)

试试这个:

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