hsym
hsym

Reputation: 5477

What is the route helper for Rails update action?

I want to put a route helper in my form helper that goes to the update action:

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

but when I run rake routes I don't see a helper for 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

The form helper in question comes from Railscasts#383's source. I found that the uploader form is pretty useful for creating a new model object but I'm struggling to get it to work for updating a model object.

When I tried the route helper shops_url, it runs a failed POST action:

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>

Any help?

Upvotes: 3

Views: 8419

Answers (2)

ekampp
ekampp

Reputation: 1959

While HTTP and rack support the use of the PUT method, the browsers don’t. So in order to spoof a put request, you need to add a _method=put parameter to the url you'r posting to.

A link in rails would look something like:

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

Upvotes: 5

Phobos98
Phobos98

Reputation: 456

The same as for show - "shop_path", as it refers to the same URL. Different is only the method. Theese Rails route helpers are pointing only for urls, but not it's methods, that's why they are the same in this case. By the way - the method should be "put:", not "post:" (as a param for your form helper)

Upvotes: 3

Related Questions