Rails routes: GET without param :id

后端 未结 7 1018
名媛妹妹
名媛妹妹 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:20

    You can use

    resources :users, only: [:index, :update] do
      get :me, on: :collection
    end
    

    or

    resources :users, only: [:index, :update] do
      collection do
        get :me
      end
    end
    

    "A member route will require an ID, because it acts on a member. A collection route doesn't because it acts on a collection of objects. Preview is an example of a member route, because it acts on (and displays) a single object. Search is an example of a collection route, because it acts on (and displays) a collection of objects." (from here)

提交回复
热议问题