[英]index is getting called instead of show
I'm trying to write an API using Ruby on Rails. 我正在尝试使用Ruby on Rails编写API。 In my controller class index method is getting called instead of show . 在我的控制器类中, 索引方法被调用而不是show 。 Although I am passing parameters. 虽然我传递参数。
tried these urls 尝试了这些网址
http://localhost:3000/api/v1/quatertodate/?invoiceStatus=PENDING
http://localhost:3000/api/v1/quatertodate/PENDING
http://localhost:3000/api/v1/quatertodate/:PENDING
In all of the above mentioned cases my index method is getting called instead of show . 在上述所有情况下,都将调用我的index方法而不是show 。
Respective Controller class 各自的控制器类
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
end 结束
NOTE: The commented portion #invoices1 doesn't work either. 注意: #invoices1注释部分也不起作用。 Throws: 抛出:
<ActiveRecord::RecordNotFound: Couldn't find Invoice with 'customerId'=>
Schema 架构
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"
Possible invoiceStatus values: BILLED or PENDING 可能的invoiceStatus值: BILLED或PENDING
routes 路线
Rails.application.routes.draw do
namespace 'api' do
namespace 'v1' do
resources :invoices
resources :monthtodate
resources :quatertodate
resources :yeartodate
end
end
end
My objective: To return invoices from last 90 days whose invoice status is PENDING. 我的目标是:退回最近90天的发票状态为“待处理”的发票。
I also have tried update method for PATCH request, instead of show but it is throwing this error 我也尝试过PATCH请求的更新方法,而不是show,但是它抛出此错误
AbstractController::ActionNotFound: The action 'update' could not be found for Api: AbstractController :: ActionNotFound:找不到针对Api的操作“更新”:
Removing index method gives following error: 删除索引方法会产生以下错误:
ActiveRecord::RecordNotFound: Couldn't find Invoice with 'customerId'=>" ActiveRecord :: RecordNotFound:找不到带有'customerId'=>“的发票
What am I missing? 我想念什么? Can anyone direct me towards right direction? 谁能指引我正确的方向? I couldn't find any related docs either. 我也找不到任何相关文档。
I'm new to Ruby on Rails. 我是Ruby on Rails的新手。
If you're going to follow proper Rails and REST conventions you wouldn't use the show
action for this. 如果要遵循正确的Rails和REST约定,则不会为此使用show
操作。 show
is for showing one record, and is called with a URL of this format api/v1/quartertodate/:id
, where :id
is a dynamic variable for the id
of the record you want to show. show
用于显示一条记录,并使用api/v1/quartertodate/:id
格式的URL进行调用,其中:id
是要显示的记录的id
的动态变量。 In the controller it is available as params[:id]
. 在控制器中,它可以作为params[:id]
。
index
is for showing many records, even if it's not all of the records. index
用于显示许多记录,即使不是所有记录也是如此。
You can handle this with an if...else
branch in the index action. 您可以在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
Notes: You don't need select('*')
, and you should be using an instance variable for @invoices
because it will require less refactoring if you ever switch to a templating engine for your JSON API. 注意:不需要select('*')
,并且应该为@invoices
使用实例变量,因为如果您为JSON API切换到模板引擎,则重构所需的内容会更少。 Also, you don't need a global variable for today's date, just use the built in Date
library and do Date.today
. 另外,您不需要今天的日期的全局变量,只需使用内置的Date
库并执行Date.today
。 To find the date 90 days ago you can create a private method ninety_days_past
so you're not duplicating the code. 要查找90天之前的日期,您可以创建一个私有方法ninety_days_past
因此您无需复制代码。 If you want this method in every controller just define that method directly in your ApplicationController
instead of your QuatertodateController
. 如果要在每个控制器中使用此方法,只需在ApplicationController
而不是 QuatertodateController
直接定义该方法。
This url will reuest the index action: 此网址将重新索引操作:
http://localhost:3000/api/v1/quatertodate?invoiceStatus=PENDING
Route next identifies the string ?invoiceStatus=PENDING as id parameter. 路由下一个将字符串?invoiceStatus = PENDING标识为id参数。 So it will request the show action. 因此它将请求显示动作。
http://localhost:3000/api/v1/quatertodate/?invoiceStatus=PENDING
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.