我想在我的表单助手中放置一个路由助手,该向导转到update操作:

<%= s3_uploader_form post: <route helper goes here>, as: "shop[logo_ori]" do %>
  <%= file_field_tag :file %>
<% end %>

但是当我运行rake routes时,我没有看到PUT的帮助程序:
shops     GET    /shops(.:format)                     shops#index
          POST   /shops(.:format)                     shops#create
new_shop  GET    /shops/new(.:format)                 shops#new
edit_shop GET    /shops/:id/edit(.:format)            shops#edit
shop      GET    /shops/:id(.:format)                 shops#show
          PUT    /shops/:id(.:format)                 shops#update

有问题的表单帮助程序来自Railscasts#383的source。我发现上载器表单对于创建新的模型对象非常有用,但是我正在努力使其能够用于更新模型对象。

当我尝试路由助手shops_url时,它运行失败的POST操作:
Started POST "/shops" for 127.0.0.1 at 2012-12-27 01:10:22 +0800
Processing by ShopsController#create as */*
Parameters: {"shop"=>{"logo_ori"=>"https://bucket.s3.amazonaws.com/example.gif"}}
User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
(0.1ms)  BEGIN
(0.1ms)  ROLLBACK
<additional output redacted>

有什么帮助吗?

最佳答案

HTTP和机架支持使用PUT方法,而浏览器则不支持。因此,为了欺骗放置请求,您需要在要发布到的URL上添加_method=put参数。

Rails中的链接看起来像:

<%= link_to "update me", "/link/to/resource", method: :put %>

07-28 02:27
查看更多