2012-12-26 26 views
1

我想提出一個路線幫手在我的幫助形式行進至update行動:什麼是Rails更新操作的路由幫助器?

<%= 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> 

任何幫助嗎?

+0

'shops_url'看起來不錯,'ShopsController#create'被調用。什麼是錯誤? – Baldrick

+1

@Baldrick我希望它指向更新操作,而不是創建。 –

+0

當創建** POST /資源**時,更新路由*絕對不是*與創建路徑*相同,更新爲** POST/resources/id ** 您想要的是'shop_path(shop)'或'shop_path(shop_id)' – systho

回答

2

與show - 「shop_path」相同,因爲它指的是相同的URL。不同的只是方法。 Theese Rails路由幫助程序僅指向url,但不是它的方法,這就是爲什麼它們在這種情況下是相同的。 順便說一下 - 方法應該是「put:」,而不是「post:」(作爲你的表單助手的參數)

+0

我試過了,但不起作用。我開始懷疑它仍然指向CREATE操作,因爲它在coffeescript中使用了jQuery $ .post:https://github.com/railscasts/383-uploading-to-amazon-s3/blob/master /gallery-jquery-fileupload/app/assets/javascripts/paintings.js.coffee –

3

雖然HTTP和機架支持使用PUT方法,但瀏覽器不支持。因此,爲了欺騙投放請求,您需要爲您發佈的網址添加_method=put參數。

在軌的鏈接看起來是這樣的:

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

更新:現在,當使用rails時,這是作爲javascript調用完成的,因此它實際上是一個真正的HTTP PUT調用。 –