【发布时间】:2017-08-08 11:54:38
【问题描述】:
我正在尝试使用 Ruby on Rails 编写 API。在我的控制器类中,index 方法被调用而不是 show。虽然我正在传递参数。
试过这些网址
http://localhost:3000/api/v1/quatertodate/?invoiceStatus=PENDING
http://localhost:3000/api/v1/quatertodate/PENDING
http://localhost:3000/api/v1/quatertodate/:PENDING
在上述所有情况下,我的 index 方法被调用而不是 show。
相应的控制器类
module Api
module V1
class QuatertodateController < ApplicationController
def index
invoices = Invoice.select("*").where("invoiceDate >= ?", @@present_date-90)
render json: {status: 'SUCCESS', messasge: 'LOADED QUATERLY INVOICES', data: invoices}, status: :ok
end
def show
invoices1 = Invoice.select("*").where("invoiceStatus== ? AND invoiceDate >= ?", params[:invoiceStatus], @@present_date-90)
#invoices1 = Invoice.find(params[:invoiceStatus])
#invoices1=Invoice.select("*").where("invoiceStatus= ? and invoiceDate >= ?", params[:invoiceStatus], @@present_date-180)
render json: {status: 'SUCCESS', messasge: 'LOADED QUATERLY INVOICES', data: invoices1}, status: :ok
end
end
end
结束
注意: 注释部分 #invoices1 也不起作用。抛出:
<ActiveRecord::RecordNotFound: Couldn't find Invoice with 'customerId'=>
架构
create_table "invoices", primary_key: "customerId", id: :integer, default: nil, force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=latin1" do |t|
t.string "customerNumber", limit: 12
t.string "customerType", limit: 5
t.string "invoiceType", limit: 5
t.decimal "invoiceAmt", precision: 65, scale: 30
t.string "invoiceStatus", limit: 12
t.datetime "invoiceDate"
可能的 invoiceStatus 值: BILLED 或 PENDING
路线
Rails.application.routes.draw do
namespace 'api' do
namespace 'v1' do
resources :invoices
resources :monthtodate
resources :quatertodate
resources :yeartodate
end
end
end
我的目标:退回过去 90 天内发票状态为待处理的发票。
我也尝试了 PATCH 请求的 update 方法,而不是 show 但它抛出了这个错误
AbstractController::ActionNotFound:找不到 Api 的操作“更新”:
删除 index 方法会出现以下错误:
ActiveRecord::RecordNotFound: 找不到带有 'customerId'=>" 的发票
我错过了什么?谁能指引我朝着正确的方向前进?我也找不到任何相关文档。
我是 Ruby on Rails 的新手。
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-3.1