So I want to do some modifications to the data only if it is a POST or a PATCH request. In this question (Ruby on Rails 3: How to retrieve POST and GET params separatly?), there is a way to get POST and GET params, but I've searched, and there doesn't seem to be a way to get PATCH data only.
Asked
Active
Viewed 922 times
2 Answers
2
ActionDispatch::Request has a bunch of methods to check HTTP verb, those include: get?
, post?
, patch?
and put?
.
So the following should do the trick:
def some_action
request.patch? # only patch requests
# or
request.request_method == :patch
# and if you are intrested in both PATCH and POST... combine them!
request.patch? || request.post?
end
Note that you might be intrested in restricting used HTTP verb at your routes definition. Defining your actions with a particular verb restricts requests which do not use the same verb from being processed:
# routes.rb
put '/orders/:id/refuse' => 'orders#refuse'
# so your refuse method accepts only PUT requests

twonegatives
- 3,400
- 19
- 30
1
I believe this will do the trick:
# in some controller's method
if request.patch? || request.post?
# do your work here
end

retgoat
- 2,414
- 1
- 16
- 21
-
So if i access the params object inside the if statement, it's guaranteed that it will only have data that is either from PATCH or POST and no GET for example ? – user2968505 Jun 22 '16 at 08:38
-
@user2968505 exatly. – retgoat Jun 22 '16 at 08:43