0

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.

Community
  • 1
  • 1
user2968505
  • 435
  • 2
  • 7
  • 18

2 Answers2

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