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



错误:No route matches [GET] "/订票/:id/%3E:format"

我想更新一个属性时,点击链接的'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 }
结束

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

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。

相关内容

  • 没有找到相关文章

最新更新