Rails:尝试通过 link_to 更新属性时出现路由错误

Rails: Route Error while trying to update attribute through link_to

错误:没有路线匹配 [GET] "/bookings/:id/%3E:format"

我想在单击 'link_to' 的 link 时更新属性..

<%= link_to 'Cancel', '/bookings/:id/(.:format)' %>

routes.rb

put '/bookings/:id/(.:format)' => "bookings#tocancel"
patch '/bookings/:id/(.:format)' => "bookings#tocancel"

控制器

def tocancel
 @booking = Booking.find(params[:id])
 @booking.update_attribute(:status, "cancel")
 respond_to do |format|
  format.html { redirect_to @booking, notice: 'Booking was successfully cancelled.' }
  format.json { render :show, status: :ok, location: @booking }

结束

在预订控制器中创建一个方法:

def tocancel
 @booking = Booking.find(params[:id])
 @booking.update_attribute(:status, "cancel")
 respond_to do |format|
  format.html { redirect_to @booking, notice: 'Booking was successfully cancelled.' }
  format.json { render :show, status: :ok, location: @booking }
 end
end

路线为:

resources :bookings do
  member do
   get :tocancel
  end
end

取消link可以创建为:

link_to "Cancel", tocancel_booking_path(booking.id)

这里你应该有 booking_id 传递给取消 link。现在检查如何在放置此取消 link 的页面上获得 booking_id。 如果有任何问题,请告诉我。