Ruby on rails RubyonRails:如何仅检索POST和PATCH参数
因此,我只想在数据是POST或补丁请求时对其进行一些修改。在这个问题()中,有一种获取POST和获取参数的方法,但我已经搜索过了,似乎没有一种方法可以只获取补丁数据。有一系列方法来检查HTTP谓词,这些方法包括:Ruby on rails RubyonRails:如何仅检索POST和PATCH参数,ruby-on-rails,http,post,httprequest,Ruby On Rails,Http,Post,Httprequest,因此,我只想在数据是POST或补丁请求时对其进行一些修改。在这个问题()中,有一种获取POST和获取参数的方法,但我已经搜索过了,似乎没有一种方法可以只获取补丁数据。有一系列方法来检查HTTP谓词,这些方法包括:get?,POST?,PATCH?和put? 因此,以下几点应该可以做到: def some_action request.patch? # only patch requests # or request.request_method == :patch # and i
get?
,POST?
,PATCH?
和put?
因此,以下几点应该可以做到:
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
# in some controller's method
if request.patch? || request.post?
# do your work here
end
请注意,您可能对限制路由定义中使用的HTTP谓词感兴趣。使用特定动词定义操作会限制处理不使用同一动词的请求:
# routes.rb
put '/orders/:id/refuse' => 'orders#refuse'
# so your refuse method accepts only PUT requests
有一系列检查HTTP谓词的方法,包括:get?
、post?
、patch?
和put?
因此,以下几点应该可以做到:
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
# in some controller's method
if request.patch? || request.post?
# do your work here
end
请注意,您可能对限制路由定义中使用的HTTP谓词感兴趣。使用特定动词定义操作会限制处理不使用同一动词的请求:
# routes.rb
put '/orders/:id/refuse' => 'orders#refuse'
# so your refuse method accepts only PUT requests
我相信这会奏效:
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
# in some controller's method
if request.patch? || request.post?
# do your work here
end
我相信这会奏效:
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
# in some controller's method
if request.patch? || request.post?
# do your work here
end
因此,如果我访问if语句中的params对象,可以保证它只包含来自PATCH或POST的数据,而不包含GET(例如?@user2968505)。因此如果我访问if语句中的params对象,它保证只包含来自补丁或POST的数据,并且没有GET,例如?@user2968505。