开发者

Get and post request for the same match

开发者 https://www.devze.com 2023-03-26 22:27 出处:网络
In my routes file I have: match \'graphs/开发者_开发百科(:id(/:action))\' => \'graphs#(:action)\'

In my routes file I have:

match 'graphs/开发者_开发百科(:id(/:action))' => 'graphs#(:action)'

and I would like to match this if it is GET request (working) or POST request (not working)

I know that I can declare POST request inside a resource using:

post '/' => :show, :on => :member

But how can I do that for a match ?

Thanks.


if you want for both POST and GET

match 'graphs/(:id(/:action))' => 'graphs#(:action)', :via => [:get, :post]

Edit

defaults can be set as following

match 'graphs/(:id(/:action))' => 'graphs#(:action)', :via => [:get, :post],
                                                      :defaults => { :action => "index" }

and the syntax seems to be correct


if you only need the match to respond to a single HTTP verbs or method (GET, POST, PUT, DELETE, PATCH being the most common ones) then using a syntax similar to your "post" example works and is more readable.

match 'graphs/(:id(/:action))' => 'graphs#(:action)'

can become

post 'graphs/(:id(/:action))' => 'graphs#(:action)' if you want to match and limit to the http POST method.

or

get 'graphs/(:id(/:action))' => 'graphs#(:action)' if you want to match and limit to the http Get method.

if you need to respond to several http verbs then the "via:" syntax is more readable.

0

精彩评论

暂无评论...
验证码 换一张
取 消