我有一个客户控制器来处理请求客户资源属于公司。我希望能够通过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