2017-06-18 6 views
0

私はを持っていますに所属する顧客リソースのリクエストを処理するための得意先コントローラです。私はGETの顧客の両方にcustomer_idまたはphone_numberのいずれかを使用できるようにしたいと思います。 デフォルトGETメソッドでは、customer_idcompany_idにアクセスできます。 ルートを変更するにはどうすればよいですか?Rails APIのカスタムURLパラメータ

はここroutes.rbをファイルに私お客様のコントローラー

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 

ためのコードです:

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 

ですから、API呼び出しでidまたはmobile_numberのいずれかを使用することができます。例えば、idを使用して:

api/v1/companies/customer/1 

または、mobile_numberを使用して:

api/v1/companies/customer/55555555 

ここでの唯一の問題は、idが数値であるとmobile_numberは数字だけを持っている場合、その後、あなたは最終的に持つことができる、ということになりますparams[:id]で、Customerがあり、idmobile_numberの両方があります。 mobile_numberによって見つけられたCustomerを返します。

これが問題を表している場合、あなたはパラメータ(クエリ文字列によって)と同様のセットアップは、現在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 

次に、あなただけのcustomersためgetデフォルトを変更するには、あなたのルートを変更する必要があります:

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

とAPIコールshoul d呼び出されているパラメータを指定します。例えば、idを求めて:

api/v1/companies/customer?id=1 

mobile_numberを尋ねる:

api/v1/companies/customer?mobile_number=55555555 

は、私は少しのパラメータとして提供有効idまたはmobile_numberを考慮するために、しかし上存在しないロジックを変更customersテーブル。

関連する問題