我正在开发一个基于 rails 的 REST api。要使用此 api,您必须登录。关于这一点,我想me
在我的用户控制器中创建一个方法,该方法将返回登录用户信息的 json。所以,我不需要:id
在 URL 中传递。我只想打电话给http://domain.com/api/users/me
所以我尝试了这个:
namespace :api, defaults: { format: 'json' } do
scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
resources :tokens, :only => [:create, :destroy]
resources :users, :only => [:index, :update] do
# I tried this
match 'me', :via => :get
# => api_user_me GET /api/users/:user_id/me(.:format) api/v1/users#me {:format=>"json"}
# Then I tried this
member do
get 'me'
end
# => me_api_user GET /api/users/:id/me(.:format) api/v1/users#me {:format=>"json"}
end
end
end
如您所见,我的路线等待一个 id,但我想得到类似设计的东西。基于current_user
id 的东西。下面的例子:
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
在此示例中,您可以编辑当前用户密码,而无需将 id 作为参数传递。
我可以使用集合而不是成员,但这是一个肮脏的绕过......
有人有想法吗?谢谢