URL 用户 ID Rails

URL User ID Rails

我如何制作一个 url 来自:

localhost:3000/api/v1/users/1

到更通用的url:

localhost:3000/api/v1/users/(id already assumed based on the signed in user)

有没有一种方法可以让 url 转到特定的用户 ID 而无需用户实际指定 ID?是否可以仅基于已经登录的用户来假设?清楚这个问题就好了!

如果您有基于令牌的身份验证,您可以使用它们来查找当前用户。 最常见的方法是为此使用路由 /me

class Api::V1::CredentialsController < Api::V1::ApiController
  before_action :doorkeeper_authorize!
  respond_to    :json

  # GET /me.json
  def me
    respond_with current_resource_owner
  end

  private

  # Find the user that owns the access token
  def current_resource_owner
    User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
  end
end

上面的示例是使用 Doorkeeper gem.

的实现