Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Can't create a route with an prefix optional segment that contains both static and dynamic parts

#4832

I'm trying to define a route that has an optional prefix segment with a static segment, like this:

get '(/groups/:group_id)/discussions' => "discussions#index"

I would expect GET /discussions to match this with :group_id set to nil and GET /groups/1/discussions to match this with :group_id set to 1. Instead, neither of those routes match. If I define the route like this:

get '/discussions(/groups/:group_id)' => "discussions#index"

GET /discussions matches and GET /discussions/groups/1 matches setting :group_id to 1. Also this:

get '(/:group_id)/discussions' => "discussions#index"

Works, GET /discussions matches and GET /1/discussions matches setting :group_id to 1.

This seems like a bug to me, or is there a reason why this wouldn't work.

I've tested this on REE 1.8.7 on edge 85cc1fa657f441417f36998a32a6a158c2697aad

Reported by Paul Barry · June 11th, 2010 @ 07:24 PM

State: resolved
Milestone: 3.0.2
Assigned to: Andrew White Andrew White
Importance: High

Activity

  1. Paul Barry
  2. Paul Barry
    Paul Barry
    • Tag changed from rails3 routes to rails3 routes, patch

    Ok, looks like the problem is with the regexp in the normalize_path method in Mapper, it only works for routes like (/:foo)/whatever, not (/foo/:bar)/whatever. I've attached a patch that should fix it.

    June 11th, 2010 @ 10:32 PM

  3. Andrew White
    Andrew White
    • Assigned user set to Andrew White

    Paul, can you rebase your patch against the latest HEAD?

    June 25th, 2010 @ 09:29 AM

  4. Paul Barry
  5. Andrew White
    Andrew White
    • State changed from new to open
    • Milestone cleared.
    • Importance changed from to High

    Thanks Paul. I've taken your patch and extended it to also support optional static segments by themselves as well.

    June 26th, 2010 @ 10:50 AM

  6. Repository
    Repository
    • State changed from open to resolved

    (from [d4e1a2ef0d35f322803284a980575fc31ff7b4b6]) Support optional static segements as well [#4832 state:resolved]

    Signed-off-by: José Valim jose.valim@gmail.com
    http://github.com/rails/rails/commit/d4e1a2ef0d35f322803284a980575f...

    June 26th, 2010 @ 11:11 AM

  7. José Valim
    José Valim

    Both patches applied! Thanks guys!

    June 26th, 2010 @ 11:11 AM

  8. Jeremy Kemper