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

时间:2016-06-30 10:24:03

标签: ruby-on-rails ruby routes link-to update-attribute

错误:没有路线匹配[GET]" /预订/:id /%3E:格式"

我想点击链接' link_to' ..

时更新属性
<%= 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 }

1 个答案:

答案 0 :(得分:0)

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

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_to "Cancel", tocancel_booking_path(booking.id)

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