URLs like “www.example.com/courses/1;edit” and “www.example.com/
courses/edit/1” work fine, but when I use something like this
“www.example.com/courses/edit?id=1” it doesn’t work. The “show” action
is called instead of “edit”. Removing map.resources line fixes it of
course, but I was wondering if there’s a way to have the cake and eat
it too?
I need the “www.example.com/courses/edit?id=1” format to be able to
send a GET request through a form with a select_tag setting an id. So
I’d like to do something like this:
URLs like “www.example.com/courses/1;edit” and “www.example.com/
courses/edit/1” work fine, but when I use something like this
“www.example.com/courses/edit?id=1” it doesn’t work. The “show” action
is called instead of “edit”. Removing map.resources line fixes it of
course, but I was wondering if there’s a way to have the cake and eat
it too?
I need the “www.example.com/courses/edit?id=1” format to be able to
send a GET request through a form with a select_tag setting an id. So
I’d like to do something like this:
Routes match top to bottom. By putting map.resources above the
generic resource route, it matches /courses/edit as /courses/:id. Try
this:
as I said, “assuming this is an isolated case for you”, as in, if you
just wanted the behavior for a particular controller and action then
I’d deal with that specific issue rather than change route
recognition order. The fact that you had to add ‘_’ to the pattern
indicates that you had more than just the ‘edit’ action in mind so
it’s probably not an isolated case.
There’s nothing that’s specifically going to get you into trouble
later, there are plenty of pre1.2 rails apps out there that use the
default route just fine.
My personal experience has been that things are easier to control if
I don’t mix resources style urls with the default route - so lately
I’ve been deleting the default route on my new projects and only
using resources and named routes.
However, if Rick’s suggestion works for you and it doesn’t have any
side effects that you don’t like, then you’re done
My personal experience has been that things are easier to control if
I don’t mix resources style urls with the default route - so lately
I’ve been deleting the default route on my new projects and only
using resources and named routes.
I do this too to keep things consistent. However, sometimes I’m a
stickler for routes and I have custom routes on top of my resource
routes. It really depends on what I’m trying to do. If in doubt, set
up some good routing tests using assert_routing.