On Saturday, March 8, 2014 9:32:04 AM UTC-5, Arun kant sharma wrote:
>
> While using devise I have to use 
>   %li= link_to "Sign up", destroy_user_session_path, method: :delete
> now that destroy_user_session_path should automatically default to delete 
> method because that's how it is defined in routes. Why is it generate links 
> to get method for default for given path?
>

Before I answer your question, something looks a little off. 
 destroy_user_session_path would normally be used to Log Off, not for Sign 
Up.  

However, to answer your question, there are two independent actions here. 
 The first is the use of the link_to method to generate a html anchor tag. 
 In your case, this would look something like:

<a data-method="delete" href="/users/sign_out" rel="nofollow">Sign Up</a>

 This gets passed to the browser as part of the html for the page.  

Now, let's assume the user clicks on this link.  This is generates a 
completely separate action from the one above.

The browser now processes the anchor link.  However, in HTML, anchor links 
(the <a ... >) only support GET method requests and no  matter what you put 
in there, the request generated will be a GET.  Rails has written 
javascript to intervene, detect the "data-method" and generate a request 
with the appropriate method (in this case, delete).  If javascript is 
disabled or there's an error, it will get sent as a GET request.

Therefore, the following information gets passed to the routing table:

URL:  /users/sign_out
METHOD:  Delete

It looks for a route matching both of those parameters and routes 
accordingly. Please note the routing table does not define methods for a 
request, it uses the method passed in a request in combination with the URL 
to determine the appropriate route for the request (or reject it if no rule 
is defined).  You might want to read (or re-read) the following:

http://edgeguides.rubyonrails.org/routing.html


-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-talk+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/ca12c985-a8c3-4b00-86ee-a371a1618413%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to