我正在尝试弄清楚如何获得与 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'
由资源的优先级高于稍后定义的匹配“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/