【问题标题】:Rails: Route Error while trying to update attribute through link_toRails:尝试通过link_to更新属性时出现路由错误
【发布时间】:2016-06-30 10:24:02
【问题描述】:

错误:没有路线匹配 [GET] "/bookings/: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 }

结束

【问题讨论】:

  • 您没有将 URL 格式传递给 link_to。您可以使用辅助方法 bookings_path 并传递 @booking 对象。阅读有关它的(优秀)文档:guides.rubyonrails.org/routing.html
  • &lt;%= link_to 'Cancel', 'complete_url/bookings/pass_id_value_here' %&gt;:id 地方传递 id 值。
  • 错误:没有路线匹配 [GET] "/bookings/36/tocancel"

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


【解决方案1】:

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

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。 如果有任何问题,请告诉我。

【讨论】:

  • 出现错误:#:0x007fe38405a900> 的未定义局部变量或方法“booking_id”
  • 你能给我你添加这个链接的页面的网址吗?您需要在该页面中检索 booking_id 并传递给此取消链接..
  • 这里是链接 active.html.erb [链接] (drive.google.com/file/d/0B-cw8FVV0gUeSENXWGlrbW1HNVU/…)
  • 链接似乎是正确的...尝试传递预订 ID 而不是像 booking.id 这样的对象
  • 我通过了预订,然后它接受了 id,但没有显示路线数学。错误:没有路线匹配 [GET] "/bookings/36/tocancel"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多