gpt4 book ai didi

ruby-on-rails - 具有自定义 View 的路线

转载 作者:数据小太阳 更新时间:2023-10-29 08:50:32 24 4
gpt4 key购买 nike

我正在尝试弄清楚如何获得与 Rails 一起使用的自定义路由来执行日期查询。如果可能的话,我想调用这份报告。所以我在浏览器上访问我的路线并获得以下信息。

http://localhost:3000/trips/report

Couldn't find Trip with id=report

就像它试图在报告中读入 ID 一样?我不太清楚我哪里出了问题?在我的 routes.rb 文件中,我创建了以下条目。

match 'trips/report'    =>  'trips#report'

在我的行程 Controller 中使用以下内容。

def report
@trips.all :condition => ["DATE(date) = DATE(?)", Time.now]

respond_to do |format|
format.html
format.json { render json: @trips }
end
end

我可能正在做一些非常愚蠢的事情!希望有人能帮助我走上正确的道路?

最佳答案

您肯定已经在您的 routes.rb 中声明了一个 trips 资源,并且路线 GET 'trips/:id' 由资源比后面定义的 match 'trips/report' 具有更高的优先级(Rails 使用第一个匹配规则)。如果是这种情况,请像这样声明您的 report 路由:

resources trips do 
collection do
get 'report'
end
end

this chapter in Rails Routing Guide获取更多信息。

关于ruby-on-rails - 具有自定义 View 的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14161859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com