私はを持っていますに所属する顧客リソースのリクエストを処理するための得意先コントローラです。私はGETの顧客の両方にcustomer_idまたはphone_numberのいずれかを使用できるようにしたいと思います。 デフォルトGETメソッドでは、customer_idとcompany_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
サンプルレスポンスはどのようになりますか?私たちを見せてもらえますか? – Pavan
{ "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
これは、顧客の詳細を含むJSON応答です。 – Paras