0

我想用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 

耙路線 route

我的目標:要從其發票狀態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的新手。

回答

1

如果你要遵循正確的Rails和REST約定你不會使用這個show行動。 show是顯示一個記錄,被稱爲用這種格式api/v1/quartertodate/:id,其中:id是要顯示的記錄的id一個動態變量的URL。在控制器中,它可以用作params[:id]

index用於顯示多條記錄,即使它不是全部是的記錄。

您可以在索引操作的if...else分支處理這個問題。

def index 
    @invoices = if params[:invoiceStatus].present? 
    Invoice.where("invoiceStatus = ? AND invoiceDate >= ?", params[:invoiceStatus], ninety_days_past) 
    else 
    Invoice.where("invoiceDate >= ?", ninety_days_past) 
    end 

    render json: {status: 'SUCCESS', messasge: 'LOADED QUATERLY INVOICES', data: @invoices}, status: :ok 
end 

def show 
    @invoice = Invoice.find(params[:id]) 
    render json: {status: 'SUCCESS', messasge: 'LOADED INVOICE', data: @invoice}, status: :ok 
end 

private 

def ninety_days_past 
    Date.today - 90 
end 

注意:您不需要select('*'),你應該使用實例變量@invoices,因爲它需要較少的重構如果你切換到一個模板引擎爲您的JSON API。此外,您今天的日期不需要全局變量,只需使用內置的Date庫並執行Date.today即可。要找到90天前的日期,您可以創建一個私人方法ninety_days_past,這樣您就不會複製代碼。如果你想在每一個控制器這種方法只是直接在ApplicationController而不是QuatertodateController定義方法。

+0

作品魅力!你可以請參考我的任何好的(初學者友好)文檔/會談? – BlackBeard

+0

@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 –

0

此URL將reuest索引操作:

http://localhost:3000/api/v1/quatertodate?invoiceStatus=PENDING 

路由下標識字符串invoiceStatus = PENDING作爲ID參數。所以它會要求演出。

http://localhost:3000/api/v1/quatertodate/?invoiceStatus=PENDING 
+0

那麼,對於我的問題,正確的解決方案是什麼?我怎樣才能正確地傳遞參數給_show_? – BlackBeard

+1

將變量傳遞給生成的路徑api_v1_ quatertodate_path(@quatertodate),它將生成類似http:// localhost:3000/api/v1/quatertodate/1的路由,最後1是記錄的id。 – xiaocui

+0

你會好好闡述一下嗎?或者給我一個例子或一些參考? – BlackBeard

相關問題