Rails routes: GET without param :id

后端 未结 7 985
名媛妹妹
名媛妹妹 2021-01-01 08:56

I\'m developing a REST api based on rails. To use this api, you MUST be logged in. Regarding that, I\'d like to create a method me in my user controller that wi

7条回答
  •  被撕碎了的回忆
    2021-01-01 09:31

    The way to go is to use singular resources:

    So, instead of resources use resource:

    Sometimes, you have a resource that clients always look up without referencing an ID. For example, you would like /profile to always show the profile of the currently logged in user. In this case, you can use a singular resource to map /profile (rather than /profile/:id) to the show action [...]

    So, in your case:

    resource :user do
      get :me, on: :member
    end
    
    # => me_api_user GET    /api/users/me(.:format)            api/v1/users#me {:format=>"json"}
    

提交回复
热议问题