具有自定义视图的路由

时间:2013-01-04 17:27:18

标签: ruby-on-rails ruby controller routes

我正在尝试找出如何使用rails来执行日期查询的自定义路由。如果可能的话,我想打电话给这份报告。所以我在浏览器上访问我的路线并获得以下内容。

http://localhost:3000/trips/report

Couldn't find Trip with id=report

就像它试图在报告中读取ID一样?我无法解决我在这里出错的地方?在我的routes.rb文件中,我创建了以下条目。

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

在我的行程控制器中有以下内容。

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

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

我可能做得很傻!希望有人可以帮助我走上正轨吗?

1 个答案:

答案 0 :(得分:3)

您当然已在trips中声明了routes.rb资源,并且资源生成的路由GET 'trips/:id'的优先级高于之后定义的match 'trips/report'(Rails使用第一个匹配规则)。如果是这种情况,请声明您的report路线:

resources trips do 
  collection do
    get 'report'
  end
end 

请查看this chapter in Rails Routing Guide以获取更多信息。

相关问题