Abram
Abram

Reputation: 41884

Custom route mistaken for object id in Rails

I have the following route:

 view_all_styles        /styles/view_all(.:format)                styles#view_all

When I point my broswer at xyz.com/styles/view_all I receive the error:

ActiveRecord::RecordNotFound at /styles/view_all
Couldn't find Style with id=view_all

I'm also routed to the show action??

Request parameters  
{"action"=>"show", "controller"=>"styles", "id"=>"view_all"}

Upvotes: 1

Views: 126

Answers (1)

arbylee
arbylee

Reputation: 2099

It sounds like you've got your routes defined in the wrong order - you'll want to define your custom route before the resource routes of styles. Otherwise, you'll run into exactly this problem.

Since your route, /styles/view_all also fits into the route for #show, /styles/:id ('view_all' being the :id), it will match and pass along the request to #show before it even tries to match your custom route.

Upvotes: 3

Related Questions