2
<% form_ tag user_path(@user), :method => :put do %>

That's my form, so I want it to access the update method of my UsersController, I set the map.resources :users , and the RESTful paths generated:

users     GET    /users(.:format)          {:action=>"index", :controller=>"users"}          
          POST   /users(.:format)          {:action=>"create",:controller=>"users"}
new_ user GET    /users/new(.:format)      {:action=>"new", :controller=>"users"}
edit_user GET    /users/:id/edit(.:format) {:action=>"edit", :controller=>"users"}
user      GET    /users/:id(.:format)      {:action=>"show", :controller=>"users"}
          PUT    /users/:id(.:format)      {:action=>"update", :controller=>"users"}
          DELETE /users/:id(.:format)      {:action=>"destroy", :controller=>"users"}

So I try to send to user_path(@user) using the PUT HTTP method and it comes back with:

Unknown action

No action responded to 1. Actions: create, destroy, edit, index, logged?, new, show and update

So obviously I don't know how to make this work, so thanks in advance.

usha
  • 28,973
  • 5
  • 72
  • 93

5 Answers5

5

If you're using RESTful resources (and you should be), try using form_for not form_tag ... with the full setup like this:

<% form_for :user, @user, :url=>user_path(@user), :html=>{:method=>:put} do |f| %>

  #this scopes the form elements to the @user object, eg.
  <%= f.text_field :first_name %>

<% end %>

Check out the API docs for more.

nisevi
  • 627
  • 1
  • 10
  • 27
mylescarrick
  • 1,680
  • 8
  • 10
2

Too late to answer, but check this out http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-put-or-delete-methods-work

Not all browsers support PUT so you use POST with a hidden input stating that the method is PUT

Khaled
  • 2,101
  • 1
  • 18
  • 26
1

Just a shot in the dark, but have you tried this?

<% form_tag :url=>user_path(@user), :html=>{:method=>:put} do %>
Scott
  • 654
  • 5
  • 10
  • Updated link : http://guides.rubyonrails.org/form_helpers.html#how-do-forms-with-patch-put-or-delete-methods-work-questionmark – Stéphane V Sep 22 '13 at 07:44
0

Did you restart your server? My routes.rb never gets reloaded correctly if I update it while the server is running.

aehlke
  • 15,225
  • 5
  • 36
  • 45
0

I ran into this exact problem when trying to use a tableless model (Rails model without database).

After some quick digging into actionpack's form_helper.rb I found a solution. Add this to your model:

def new_record?; false; end

In my case my model is always built from scratch so this was necessary to "trick" Rails into treating it as an existing object, and thus doing a PUT rather than a POST.

Community
  • 1
  • 1
alalonde
  • 1,823
  • 1
  • 16
  • 27