Reputation: 303
I'm trying to work out how to get a custom route working with rails to perform a date query. I'd like to call this report if possible. So I access my route on the browser and get the following.
http://localhost:3000/trips/report
Couldn't find Trip with id=report
Like it's trying to read in report as an ID? I can't quite work out where I've gone wrong here? In my routes.rb
file I've create the following entry.
match 'trips/report' => 'trips#report'
With the following in my trips controller.
def report
@trips.all :condition => ["DATE(date) = DATE(?)", Time.now]
respond_to do |format|
format.html
format.json { render json: @trips }
end
end
I'm probably doing something very silly! Hopefully someone can help me along the right track?
Upvotes: 2
Views: 69
Reputation: 24340
You certainly have declared a trips
resource in your routes.rb
, and the route GET 'trips/:id'
generated by the resource have a higher priority then the match 'trips/report'
defined later (Rails uses the first matching rule). If it's the case, declare your report
route like this:
resources trips do
collection do
get 'report'
end
end
Look at this chapter in Rails Routing Guide for more information.
Upvotes: 3