0

On the Rails Routing from the outside in page, in section 2.2, there's talk of how the http verbs and URLs are used to match 4 URLs to 7 paths.

In section 2.3 it explains how helper paths are available and, sure enough, there are the 4 paths that appear to correspond with those in the table in section 2.2.

I'd like to know what determines which VERB is used when a path is called. For instance, say I have resource :photos and I call:

redirect_to photo_path(10)

WHAT tells me which of the 3 available verbs for that option (GET, PUT/PATCH or DELETE - according to the table in section 2.2 above) will be included as part of the route?

moosefetcher
  • 1,841
  • 2
  • 23
  • 39

1 Answers1

1

Path is path, it doesn't include VERB (HTTP METHOD) information. For example, path to show and destroy resource actions are by default the same and you use the same path helper (but different HTTP method):

<%= link_to 'show photo', photo_path(photo) %> <!-- returns 'default' link, so GET method is used here -->
<%= link_to 'delete photo', photo_path(photo), method: :delete %>

Redirects are performed always with get.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • OK, but those 2 lines DO different things, right? How do they trigger different actions if the paths are the same? Is it the `method :delete` that makes the difference? – moosefetcher Dec 12 '14 at 15:01
  • @moosefetcher yes, `method: :delete` adds attribute `data-method="delete"` to `a` tag, which indicates to the browser that it should use `DELETE` method. – Marek Lipka Dec 12 '14 at 15:03
  • Thank you. So in SOME cases you CAN control which verb is used. I'm guessing there's a whole bunch of these arbitrary facts I need to pick up - eg: 'Redirects are always performed with GET'. Rails is hugely esoteric and obfuscated at the moment. I'm guessing I'll need to ask similar questions for any number of other uni-task examples. But thank you for your reply. – moosefetcher Dec 12 '14 at 15:08
  • @moosefetcher In most cases you can control which verb is used. There are, of course, some defaults, for example by default `form` tag uses `POST` method and regular links use `GET`, but you can change this behavior. You can't change only the method in redirect, which is always `GET`. And BTW Rails has nothing to do with this behavior - it's about HTTP protocole. – Marek Lipka Dec 12 '14 at 15:12
  • But it's Rails that seems to HIDE the information in its 'convention over configuration'. No, I don't want to have to code every last thing. I just wish the information was explained in a way that's useful for newbies. I've only just recently realised that paths effectively point to routes. Everyone was talking about checking rake routes, but I didn't even know how those routes were accessed. It's very frustrating. – moosefetcher Dec 12 '14 at 15:16
  • No, I don't think it's *hiding* - if you don't add proper attribute, `a` tag uses `GET` and forms use `POST`. It's 'hidden' in HTML, not in Rails. – Marek Lipka Dec 12 '14 at 15:19
  • Just as a 'by the way' - I wasn't saying that Rails hides CODE; I'm saying the specific knowledge of what Rails does behind the scenes (usually by default) is hard to access when you're learning. I think online docs should prioritise explaining what the defaults are, because it's not knowing what Rails is doing FOR me that has made it very difficult to understand. – moosefetcher Dec 12 '14 at 15:32
  • These issues are less about rails and more about the web in general, particularly, http and REST - for example, [the http standard](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3) defines redirects to be ([mostly](http://programmers.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect)) GET. Routes and the choice of http methods (GET, PUT, POST, etc.) is based on a principle of [REST](http://en.wikipedia.org/wiki/Representational_state_transfer) called [HATEOAS](http://en.wikipedia.org/wiki/HATEOAS). – Anand Dec 12 '14 at 15:42