neanderslob
neanderslob

Reputation: 2693

Button_to link results in routing error

I'm attempting to put some slick buttons on my rails app to serve as links. However, I'm running into an odd problem in my attempt to do so. I've attempted to add a link button as follows:

<%= button_to( "New", :action => "new", :controller => "registrations") %>

This results in a nice new button to direct my users to the sign_up page.

Here's where it gets weird: when I click on the button I am routed to http://localhost:3000/users/sign_up and receive the following error:

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

But this simply isn't true. In fact, I can highlight the very url that brought me to that error, copy it and paste it into a new tab and it loads fine.

To be absolutely clear, here's the path from rake routes:

new_user_registration GET    /users/sign_up(.:format)       registrations#new

What might be going on here?

Any thoughts are appreciated.

Upvotes: 0

Views: 589

Answers (2)

Salil
Salil

Reputation: 47472

Your routes expecting method get where as button_to` aren't supposed to be sending GET requests, That's creating problem.

you have to do one of the following thing

1.Change button_to to link_to

<%= link_to( "New", :action => "new", :controller => "registrations") %>

2.Add :method => :get

<%= button_to( "New", {:action => "new", :controller => "registrations"}, :method => :get) %>

Upvotes: 3

Marek Lipka
Marek Lipka

Reputation: 51151

By default clicking on button sends POST request to the server. You should change this behavior to send GET:

<%= button_to('New', {action: 'new', controller: 'registrations'}, method: :get) %>

Upvotes: 1

Related Questions