将动词添加到Rails路由的资源

时间:2015-07-14 12:55:45

标签: ruby-on-rails rest rails-routing

默认情况下,Rails路由的resources关键字会创建7个操作。

例如resources :foos

-----------------------
| Verb      | Action  |
-----------------------
| GET       | index   |
| GET       | show    |
| GET       | edit    |
| GET       | new     |
| PUT/PATCH | update  |
| POST      | create  |
| DELETE    | destroy |
-----------------------

我们如何在此列表中添加OPTIONS动词,例如:

--------------------------------
| Verb    | Action             |
--------------------------------
| ...     | ...                |
| OPTIONS | member_options     |
| OPTIONS | collection_options |
--------------------------------

换句话说,默认情况下,对于每种资源,我们都必须使用以下内容:

match    '/foos/',      via: :options, controller: 'foos', action: 'collection_options'
match    '/foos/:id/',  via: :options, controller: 'foos', action: 'member_options', as: :foo
resources :foos

相反,我更喜欢自定义设置才能执行此操作:

resources :foos

1 个答案:

答案 0 :(得分:0)

为避免列出OPTIONS动词的每个路径,您可以添加catch all route,例如

match '*path', to: 'application#cors_preflight_check', via: [:options]