# Do I have to care about other methods than GET and POST?

**URL:** <https://www.ruby-forum.com/t/do-i-have-to-care-about-other-methods-than-get-and-post/118224>\
**Category:** Rails\
**Created:** [October 24, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/do-i-have-to-care-about-other-methods-than-get-and-post/118224 "2007-10-24T23:03:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 24, 2007, 11:03pm UTC](https://www.ruby-forum.com/t/do-i-have-to-care-about-other-methods-than-get-and-post/118224/1 "2007-10-24T23:03:16Z")

</div>

Hi all

Sometimes I check in a controller if the method is GET or POST. But  
afaik there are other methods - do I have to care about them?

Is the following enough?

if request.get?  
…  
elsif request.post?  
…  
end

Or do I have to add also an else block?

if request.get?  
…  
elsif request.post?  
…  
else  
raise “Boah!”  
end

Thanks for infos  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 24, 2007, 11:25pm UTC](https://www.ruby-forum.com/t/do-i-have-to-care-about-other-methods-than-get-and-post/118224/2 "2007-10-24T23:25:49Z")

</div>

> Or do I have to add also an else block?
> 
> if request.get?  
> …  
> elsif request.post?  
> …  
> else  
> raise “Boah!”  
> end

IMO, defensive programming says there should almost always be an else  
in these type of constructs unless they’re being used to prepare  
special cases for the code that follows (code which doesn’t care if  
there’s neither a GET nor POST).

So, what will your program do if neither of the first two are  
satisfied? If the code must have one or the other, then yes, you need  
an else. If the code modifies something in the cases of GET/POST and  
otherwise runs just fine if there is no GET/POST, then you do not  
need the else (because it would halt processing you’re expecting to  
happen).

– gw

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 25, 2007, 1:25am UTC](https://www.ruby-forum.com/t/do-i-have-to-care-about-other-methods-than-get-and-post/118224/3 "2007-10-25T01:25:05Z")

</div>

Just to add to the last reply, there are now other very commonly used  
methods. Namely PUT, DELETE and sometimes HEAD. Rails 1.2 started this  
and Rails 2.0 will go beyond even what’s being done in 1.2. Not adding  
more methods, but “scaffold\_resource” is now simply “scaffold.” When  
generating scaffolding you will get RESTful controllers by default  
using the full set of HTTP methods.

However, keep in mind that if the request is being generated from a  
web form it will use GET or POST (at least until browsers get smart  
enough to handle the other methods). That being said in a modern Rails  
application you can’t count of the request coming from a HTML form. It  
just might be coming from another client that does understand the full  
REST method set, or even from a command line on the terminal.

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 26, 2007, 5:48am UTC](https://www.ruby-forum.com/t/do-i-have-to-care-about-other-methods-than-get-and-post/118224/4 "2007-10-26T05:48:36Z")

</div>

If you really need to check for specific http verbs, the else is a  
must. Browsers are not smart enough to send anything than  
GET/POST, but other HTTP clients are, e.g.: wget, curl, just to  
name a few. curl can also send invalid http verbs.

Rails provide a much better way of doing http verb checks, the  
verify method [1]. It also allow you to check for http/session  
parameters and xhr.

If verify method not suit your needs, i will try to use a more  
idiomatic  
code like this.

request\_is(request) do |verb|  
verb.post { do\_something1 }  
verb.get { do\_something2 }  
verb.head { do\_somethind3 }  
…  
end

The main idea is to encapsulate common behaviour for all actions  
in a request proxy. I mean, logging, mail to administrators, or  
whathever else you need to do, and pass the other method calls  
to the real request instance through Object#send

references:  
[1]  
[http://api.rubyonrails.com/classes/ActionController/Verification/ClassMethods.html](http://api.rubyonrails.com/classes/ActionController/Verification/ClassMethods.html)

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [October 27, 2007, 1:35pm UTC](https://www.ruby-forum.com/t/do-i-have-to-care-about-other-methods-than-get-and-post/118224/5 "2007-10-27T13:35:11Z")

</div>

Thank you, guys.
