Routes with names that end in _path

Small thing, but this trips newbies up a bit: Rails Routing from the Outside In — Ruby on Rails Guides

I’ve seen lots of routes that look like:

get 'exit', to: 'sessions#destroy', as: :logout_path

And then calls to logout_path in views not working, until someone reminds them that _path and _url get added on to the end of the alias.

Ideas:

  • Have something on the default error screen that calls out this specific mistake.
  • Have a warning in the server logs if a route alias ends with _path.
  • ???
5 Likes

I like an idea of a warning in the server logs. I think the route file is parsed fairly early, so we might even be able to manage this at server startup.

How about just treating those like if they were defined without this _path suffix.

By that I mean that all those 3 definitions become equivalent aliases of the same route named logout

get 'exit', to: 'sessions#destroy', as: :logout_path
get 'exit', to: 'sessions#destroy', as: :logout_url
get 'exit', to: 'sessions#destroy', as: :logout

With maybe some info/warning message in logs when booting the app.

There is of course possibility that someone left the _path routes names and invokes it later as _path_path or _path_url and it make this possibly breaking change that requires being noted in Changelog but I think it should be rare enough case that changing this behaviour will not wreck too much havoc in general.

1 Like

My only concern with the server logs approach is that in my experience newbies never look there (but maybe we should think of a way to address that). I would prefer to handle this with the view code raises - rather than saying "undefined method logout" we could have a more useful nudge right there in the browser. But I’m happy with anything that makes this easier!

1 Like