繁体   English   中英

Rails REST API的自定义URL参数

[英]Custom URL Param for Rails REST API

我有一个Customer控制器来处理对属于Company的Customer资源的请求。 我希望能够同时通过customer_idphone_number 获取客户 默认的GET方法只能使我访问customer_idcompany_id 我应该如何修改我的路线以进行管理?

这是我的客户控制器的代码

class Api::V1::CustomersController < ApplicationController
  respond_to :json

  def show
    customer_id = params[:id]
    mobile_number = params[:mobile_number]
    if mobile_number
      customer = Customer.find(mobile_number)
      render json: customer, status: 201
    else
      if customer_id
        customer = Customer.find(customer_id)
        render json: customer, status: 201
      else
        render json: { errors: "Customer ID and Phone Number is NULL" }, status: 422
      end
    end
  end
end

route.rb文件:

Rails.application.routes.draw do
  devise_for :users
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
  # API routes path
  get '/', :to => redirect('/index.html')
  namespace :api, defaults: { format: :json } do
    namespace :v1 do
      resources :users, only: [:show, :create, :update, :index]
      resources :sessions, only:[:create, :destroy]
      resources :companies, only:[:create, :destroy] do
        resources :products
        resources :customers do
          resources :invoices
        end
        resources :categories do
          resources :products
        end
      end
      resources :products, only:[:create, :destroy]
    end
  end
end

默认的GET方法只能让我访问customer_id和company_id。

实际上,它定义了参数的名称,但是您可以随时使用它。 在您的情况下,可以在idmobile_number使用params[:id]进行搜索,例如:

class Api::V1::CustomersController < ApplicationController
  respond_to :json

  def show
    customer = Customer.find_by(mobile_number: params[:id])

    if customer.empty?
      customer = Customer.find(params[:id])
    end

    if customer.present?
      render json: customer, status: 201
    else
      render json: { errors: "Customer ID and Phone Number is NULL" }, status: 422
    end
  end
end

因此,您可以在api调用中使用idmobile_number 例如,使用id

api/v1/companies/customer/1

或者,使用mobile_number

api/v1/companies/customer/55555555

这里唯一的问题是,如果id是一个数字,而mobile_number只有数字,那么您最终可能会有一个params[:id]来查找同时具有idmobile_numberCustomer 它将返回由mobile_number找到的Customer

如果这代表问题,那么您可以接收命名参数(通过查询字符串)和与控制器上当前类似的设置1 ,只需解决在使用mobile_number时查询Customer

class Api::V1::CustomersController < ApplicationController
  respond_to :json

  def show
    customer_id = params[:id]
    mobile_number = params[:mobile_number]

    if mobile_number
      customer = Customer.find_by(mobile_number: mobile_number)
    else
      customer = Customer.find(customer_id)
    end

    if customer.present?
      render json: customer, status: 201
    else
      render json: { errors: "Customer ID and Phone Number is NULL" }, status: 422
    end
  end
end

然后,你将只需要修改你的路由改变get默认customers

resources :customers, except: [:show] do
  resources :invoices
  get '/customer', to: `customer#show'
end

并且api调用应指定要调用的参数; 例如,要求id

api/v1/companies/customer?id=1

要求mobile_number

api/v1/companies/customer?mobile_number=55555555

1 我稍微更改了逻辑以说明作为参数提供的有效idmobile_number ,但在customers表上不存在。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM