我在Rails中创建一个REST服务。这是我的路线。
resources :users
match '/users', :controller => 'users', :action => 'options', :constraints => {:method => 'OPTIONS'}
我能够[获取]我的用户。我正在尝试更新我的用户,但收到错误:
ActionController::RoutingError (No route matches [OPTIONS] "/users/1"):
当我在rake routes
运行时,这里是我给出的路线:
users GET /users(.:format) users#index
POST /users(.:format) users#create
new_user GET /users/new(.:format) users#new
edit_user GET /users/:id/edit(.:format) users#edit
user GET /users/:id(.:format) users#show
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
/users(.:format) users#options {:method=>"OPTIONS"}
有人可以告诉我如何修复我的路线,以便我可以进行任何类型的REST呼叫吗?感谢。
答案 0 :(得分:7)
match '/users' => "users#options", via: :options
如果放在其他路线之前,也可能是一条路线。
答案 1 :(得分:3)
我无法路由请求的原因是我的match
中没有用户ID。我添加了一行:
match '/users/:id', :controller => 'users', :action => 'options', :constraints => {:method => 'OPTIONS'}
现在我可以路由我的所有GET请求。
答案 2 :(得分:3)
如果您不想为/users
和/users/id
创建另外两条路线,则可以执行以下操作:
match 'users(/:id)' => 'users#options', via: [:options]
在这种情况下,id
成为可选项,/users
和/users/id
都会响应相同的路线。