UncleLaz
UncleLaz

Reputation: 12381

Rails fails to recognize a PUT request

Rails gives the following error:

No route matches [POST] "/users/32"

Even though the routes, view and controller is set up correctly. What I am missing?

Environment: Rails 3.2.12, Ruby 2.0.0p0

Routes

Foo::Application.routes.draw do
  resources :users
  match '/pages/:action', controller: "pages", as: "pages"
  root to: 'pages', action: 'index'
end

Rake routes

    users GET    /users(.:format)          users#index
          POST   /users(.:format)          users#create
 new_user GET    /users/new(.:format)      users#new
edit_user GET    /users/:id/edit(.:format) users#edit
     user GET    /users/:id(.:format)      users#show
          PUT    /users/:id(.:format)      users#update
          DELETE /users/:id(.:format)      users#destroy
    pages        /pages/:action(.:format)  pages#:action
     root        /                         pages#index 

Haml

= form_tag "/#{@collection_name}/#{@entity.id}", :method => :put do
  ...

Which gets transformed into this html

<form accept-charset="UTF-8" action="/users/32" method="post">
  <div style="margin:0;padding:0;display:inline">
    <input name="utf8" type="hidden" value="✓">
    <input name="_method" type="hidden" value="put">
    <input name="authenticity_token" type="hidden" value="NnD8tNkx//5o3TQWhBYxFgYGS8kG9n+7WSeA0mon9t8=">
  </div>
  ...

Upvotes: 2

Views: 339

Answers (1)

UncleLaz
UncleLaz

Reputation: 12381

I have a show user page which renders a form with disabled inputs where all the user data is shown.

There's a button to enable all fields and allow editing the user. This button is handled by some JavaScript code and it toggles the "disabled" attribute on all form input elements.

Since the _method and all magic Rails form attributes are also inputs, my JavaScript code put a disabled attribute on them because they didn't have one.

The lesson: DO NOT PROCESS ALL FORM INPUTS WITH JAVASCRIPT, RAILS DEPENDS ON SOME OF THEM.

Upvotes: 1

Related Questions