路由错误 - 将参数传递给link_to更新操作

时间:2012-10-04 13:32:31

标签: ruby-on-rails-3 routing parameter-passing rails-routing

我有预订模式,我希望用户通过点击一个链接或另一个链接来确认/拒绝预订。我正在努力避免创建表单。

这是我尝试实现它的方式:

<% @bookings.each do |booking| %>
    <%= link_to "Confirm", appointment_booking_path(@appointment, booking, :status => "confirmed"), :method => :put, confirm: 'Are you sure?', :class => 'btn btn-success' %>
    <%= link_to "Reject", appointment_booking_path(@appointment, booking, :status => "rejected"), :method => :put, confirm: 'Are you sure?',:class => 'btn btn-danger' %>
<%end%>

点击其中一个链接时出现此错误:

NoMethodError in BookingsController#update
undefined method `[]' for nil:NilClass
app/controllers/bookings_controller.rb:64:in `update'

预订嵌套在约会中。这是我的佣金路线:

                          PUT    /appointments/:appointment_id/bookings/:id(.:format) 
appointment_booking       GET    /appointments/:appointment_id/bookings/:id(.:format)
edit_appointment_booking  GET    /appointments/:appointment_id/bookings/:id/edit(.:format)

BookingController更新操作:

line 64*    @appointment = Appointment.find(params[:booking][:appointment_id])
            @booking = Booking.find(params[:id])  

如果您需要更多信息,请与我们联系。提前谢谢。

1 个答案:

答案 0 :(得分:1)

仔细查看您的佣金路线,您会看到appointment_booking而非appointment_bookings(即s末尾没有bookings)。

您还需要传递约会实例作为第一个参数。假设您的约会被称为@appointment,请尝试:

<% @bookings.each do |booking| %>
  <%= link_to "Confirm", appointment_booking_path(@appointment, booking, :status => "confirmed"), :method => :put, confirm: 'Are you sure?', :class => 'btn btn-success' %>
  <%= link_to "Reject", appointment_booking_path(@appointment, booking, :status => "rejected"), :method => :put, confirm: 'Are you sure?',:class => 'btn btn-danger' %>
<% end %>