我有一個客戶控制器來處理請求客戶資源屬於公司。我希望能夠通過customer_id或電話號碼獲得客戶的。 默認GET方法只能讓我訪問customer_id和company_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
客戶的樣品迴應將如何?你能告訴我們嗎? – 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