Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No route matches [POST]" when changing link_to to button_to

I have this piece of code:

<%= link_to "New User", new_user_path, :class => "button"  %><br />

which works fine, but when I change it to,

<%= button_to "New User", new_user_path, :class => "button"  %><br />

I get this error

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

Any help at all will be appreciated.

like image 649
Jazz Avatar asked Sep 18 '12 10:09

Jazz


2 Answers

Jesus Rodriguez is right about POST and GET, but if you really need the button you can simply override the default method:

<%= button_to "New User", new_user_path, :class => "button", :method => :get  %>
like image 84
megas Avatar answered Sep 20 '22 23:09

megas


The "link_to" is looking for a /users/new using GET.

The "button_to" is looking for a /users/new using POST

If you create the routes for a controller using:

resources :user

By default, /users/new is a GET and not POST so, the second line doesn't find any route.

If you are thinking to change that action to POST I think that you should forget about it.

like image 36
Jesus Rodriguez Avatar answered Sep 19 '22 23:09

Jesus Rodriguez