通过link_to更新注册

时间:2017-10-04 15:28:55

标签: ruby-on-rails ruby ruby-on-rails-4

我想在不使用表单的情况下更新记录,只需使用link_to,就可以这样:

<%= link_to "Publish", pedidos_path(pedido, pedido: {Status: 3}), method: :put,:remote => true %>

但是我收到了这个错误:

Started PUT "/pedidos.21148?pedido%5BStatus%5D=3" for 127.0.0.1 at 2017-10-04 11:23:19 -0400

ActionController::RoutingError (No route matches [PUT] "/pedidos.21148"):
  actionpack (4.2.6) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
  web-console (2.3.0) lib/web_console/middleware.rb:28:in `block in call'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `catch'
  web-console (2.3.0) lib/web_console/middleware.rb:18:in `call'
  actionpack (4.2.6) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'

这是我的路线:

resources :pedidos

1 个答案:

答案 0 :(得分:0)

请使用正确的更新路线,即pedido_path代替pedidos_path

<%= link_to "Publish", pedido_path(pedido, pedido: {Status: 3}), method: :put %>

如果您想将此作为AJAX请求使用,那么您可以试试这个:

<%= link_to "Publish", pedido_path(pedido, pedido: {Status: 3}), method: :put, remote: true %>