URL用户ID Rails

时间:2016-11-26 02:30:24

标签: ruby-on-rails web rails-api

我如何制作网址:

localhost:3000/api/v1/users/1

如此更通用的网址:

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

有没有办法可以让网址转到某个用户ID而用户实际上不必指定ID?是否可以假设仅基于已签名的用户?清楚这个问题会很棒!

1 个答案:

答案 0 :(得分:1)

如果您有基于令牌的身份验证,则可以使用它们来查找当前用户。 最常见的方法是使用路由/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。

的实现