Rails RESTful routs without #new, rspec trouble
Posted
by pdkl95
on Stack Overflow
See other posts from Stack Overflow
or by pdkl95
Published on 2010-04-15T06:05:56Z
Indexed on
2010/04/15
6:13 UTC
Read the original article
Hit count: 187
I'm currently writing a Rails app, and hit a somewhat strange quirk. I have a controller PermissionsController
, which is mainly for display purposes at the moment. So my routing is locked down:
map.resources :permissions, :only => [:index, :show]
Unfortunately, when writing the tests, one of the routing tests fails:
it "does not recognize #new" do
{ :get => "/permissions/new" }.should_not be_routable
end
with the error:
Expected 'GET /permissions/new' to fail, but it routed to
{"action"=>"show", "id"=>"new", "controller"=>"permissions"} instead
Obviously, the #show action's route is matching with /permissions/:id
, which also gives the expected error Couldn't find Permission with ID=new
if you actually browse to that URL.
This is not a serious error, as it is correctly raising an exception with the bad :id parameter, but it's kind of ugly. Is there any way to actually make Rails reject that route? Some trick in the routing options that I'm missing?
I suppose I should just leave that test out and ignore it, or maybe remove the whole RESTful idea altogether and go back to a simpler map.connect 'permissions/:id'
style. I strongly suspect I'll be expanding this in the future, though, and kind of wanted to keep my controllers consistent with each other. Just having to add occasional :only or :except rules made routes.rb nice and clean...
© Stack Overflow or respective owner