Topic: button_to and link_to

Hi
I'm trying to do something that I thought would be really simple: change the sign out link in devise to a button. Can't get it to work.

link_to works fine

<%=  link_to "sign out ", destroy_user_session_path %>

gave

<a href="/users/sign_out">sign out y</a>

Ran rake routes

$ rake routes #> destroy_user_session GET    /users/sign_out(.:format)       {:action=>"destroy", :controller=>"devise/sessions"}

I had a few guesses as to what the correct syntax for button_to is, but no luck.

<%= button_to "Sign out", :action=>"destroy", :controller=>"devise/sessions", :method=> "delete" %>

Resulting html:

<form method="post" action="/users/sign_out?method=delete&amp;remote=true"  class="button_to">
<div>
<input type="submit" value="Sign out x" /><
input name="authenticity_token" type="hidden" value="Z/YJF9vuZU+xro5dE8weiWFw5IgxGhzlqsazfPUnkJU=" />
</div>
</form>

Another try

<%= button_to "Sign out", { :action=>"destroy", :controller=>"devise/sessions"}, :method=> "delete" %>

gave(This matches the doc for button_to)

<form method="post" action="/users/sign_out" data-remote="true" class="button_to">
<div>
<input name="_method" type="hidden" value="delete" />
<input type="submit" value="Sign out " />
<input name="authenticity_token" type="hidden" value="Z/YJF9vuZU+xro5dE8weiWFw5IgxGhzlqsazfPUnkJU=" />
</div>
</form>

What I understand from the button_to documentation is that I have to end up with another hidden input with the

All I get is
No route matches "/users/sign_out" # routes thinks there is such a route!

Any help would be greatly appreciated
Dukha

Re: button_to and link_to

Hi,

Can I see your config/routes.rb ?

Re: button_to and link_to

Thanks for the reply. The routes.rb file is not so interesting as it only has the standard Devise devise_for plus my test homepage. Here is the text of routes.rb

DeviseAgain::Application.routes.draw do

  devise_for :users

  get "static_pages/home"
  get "static_pages/about"
  get "static_pages/contact"
  get "static_pages/help"

  match '/contact', :to => 'static_pages#contact'
  match '/about',   :to => 'static_pages#about'
  match '/help',    :to => 'static_pages#help'

  root :to => "static_pages#contact"
end

Results of some more tests
1. From the button_to (as yesterday but with addition of :remote attr) Gives post, doesn't work.

<form method="post" action="/users/sign_out" data-remote="true" class="button_to">
<div>
<input name="_method" type="hidden" value="delete" />
<input type="submit" value="Sign out x" />
<input name="authenticity_token" type="hidden" value="bCMzEaYDfS2n3kMiZFhDVuQAIP38KEF+DC9k6VZ0T6s=" />
</div>
</form>

2. Hard coded method="get" (ie not from button_to). It works!!!

<form method="get" action="/users/sign_out"  class="button_to">
<div>
<input type="submit" value="Sign out 4 get" />
<input name="authenticity_token" type="hidden" value="w6vNLpWrzKTDWsn5XgKbt/HNQE89f3N1jezG8CdTo40=" />
</div>
</form>

3. Hard coded method="delete". It also works!! (It's not supposed to as I understand it)

<form method="delete" action="/users/sign_out"  class="button_to">
<div><input type="submit" value="Sign out 3 del" />
<input name="authenticity_token" type="hidden" value="w6vNLpWrzKTDWsn5XgKbt/HNQE89f3N1jezG8CdTo40=" />
</div>
</form>

4. Hard coded method="post" with hidden delete field. Doesn't work even though it is supposed to.

<form method="post" action="/users/sign_out"  class="button_to">
<div>
<input name="_method" type="hidden" value="delete" />
<input type="submit" value="Sign out z" />
<input name="authenticity_token" type="hidden" value="bCMzEaYDfS2n3kMiZFhDVuQAIP38KEF+DC9k6VZ0T6s=" />
</div>
</form>

Help understanding what is going on is great, but help to show how I can get button_to to give a method="get" form should do to get me going....

Many thanks

dukha