我想用Ruby on Rails編寫一個API。在我的控制器類索引方法正在調用,而不是顯示。雖然我傳遞參數。索引被調用而不是顯示
嘗試這些URL
http://localhost:3000/api/v1/quatertodate/?invoiceStatus=PENDING
http://localhost:3000/api/v1/quatertodate/PENDING
http://localhost:3000/api/v1/quatertodate/:PENDING
在所有的上述情況我指數方法獲取調用,而不是顯示。
相應的控制器類
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值:帳單或待
路線
Rails.application.routes.draw do
namespace 'api' do
namespace 'v1' do
resources :invoices
resources :monthtodate
resources :quatertodate
resources :yeartodate
end
end
end
條
我的目標:要從其發票狀態PENDING過去90天內退回發票。
我也試過更新方法PATCH要求,而不是顯示但引發此錯誤
AbstractController::ActionNotFound: The action 'update' could not be found for Api:
刪除指數方法給出了以下錯誤:
ActiveRecord::RecordNotFound: Couldn't find Invoice with 'customerId'=>"
我錯過了什麼?任何人都可以指引我走向正確的方向?我也找不到任何相關的文檔。
我是Ruby on Rails的新手。
作品魅力!你可以請參考我的任何好的(初學者友好)文檔/會談? – BlackBeard
@NiladriSekharBasu其中一些可能有用。我還建議您在瀏覽器的標籤中保留Rails指南並仔細閱讀這些指南https://www.theodinproject.com/courses/ruby-on-rails/lessons/routing - https://www.sitepoint.com/an-in-depth-look-at-basic-rails-routing/- http://rubylearning.com/blog/2014/01/29/routing-basics-ruby-on-rails-for-front-end-開發商/ - http://guides.rubyonrails.org/action_controller_overview.html - http://guides.rubyonrails.org/routing.html –