Rails更新动作的路线助手是什么?



我想在我的表格助手中放一个路线助手,该助手进入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的来源。我发现上传器表单对于创建新的模型对象非常有用,但是我正在努力使其用于更新模型对象。

我尝试了路由助手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方法的使用,浏览器不会。因此,为了欺骗PUT请求,您需要将_method=put参数添加到您发布的URL中。

导轨中的链接看起来像:

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

与显示 - " shop_path"相同,因为它指的是相同的URL。不同的方法是方法。Theese Rails路线助手仅指向URL,但不是该方法的方法,这就是为什么在这种情况下它们是相同的原因。顺便说一句 - 该方法应为" put:",而不是"帖子:"(作为您的形式助手的参数)

相关内容

最新更新