2017-06-18 73 views
0

我有一个客户控制器来处理请求客户资源属于公司。我希望能够通过customer_id电话号码获得客户的。 默认GET方法只能让我访问customer_idcompany_id。 我应该如何修改我的路线才能够管理这个?Rails REST API的自定义URL参数

下面的代码为我的客户控制器

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 

的routes.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 
+0

客户的样品回应将如何?你能告诉我们吗? – Pavan

+0

{ “ID”:1, “COMPANY_ID”:1, “名”: “李四”, “isActive”:真实, “created_at”: “2j017-06-17T08:20:26.000Z” , “的updated_at”: “2017-06-17T08:20:26.000Z”, “移动”:0, “地址”: “”, “PAN_NUMBER”:空, “tin_number”:空, “ party_name“:”“ } – Paras

+0

这将是包含客户详细信息的JSON响应 – Paras

回答

1

默认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 

所以,你可以使用任何一种id或API调用mobile_number;例如,使用id

api/v1/companies/customer/1 

或者,使用mobile_number

api/v1/companies/customer/55555555 

这里唯一的问题将是,如果id是一个数字,mobile_number只有数字,那么你可能最终有一个params[:id],它会发现一个Customeridmobile_number;其中它将返回mobile_number发现的Customer

如果这代表了一个问题,那么你可以在名为参数(通过查询字符串)和类似的设置因为目前有您的控制器上,刚修好你如何查询Customer使用mobile_number时:

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调用建议立即进行删除d指定正在调用的参数;例如,要求id

api/v1/companies/customer?id=1 

问计mobile_number

api/v1/companies/customer?mobile_number=55555555 

我改变了逻辑位,以考虑作为参数提供有效idmobile_number,但不存在于customers表。