2017-06-18 85 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表。