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.

router doesn't accept conditions regexp with start of line/string char

#4963

This gist should demonstrate the problem that I'm seeing:

http://gist.github.com/452280

I may be doing something wrong, but I believe this route constraint should be valid:

:constraints => {:id => /^\d/}

...but it crashes the app on boot. If I remove the ^ then it works fine.

This regexp works in irb:

'1-rick-roll-d' =~ /^\d/ => 0 'trevor' =~ /^\d/ => nil

I'm trying to dig around and make a failing test case, but not sure I'll succeed...

Reported by Trevor Turk · June 25th, 2010 @ 03:03 AM

State: invalid
Milestone: none
Assigned to: josh josh
Importance: High

Activity

  1. Neeraj Singh
    Neeraj Singh
    • State changed from new to open

    I am able to reproduce this case with rails edge.

    June 25th, 2010 @ 05:19 AM

  2. Trevor Turk
    Trevor Turk
    • Assigned user set to josh

    Thanks for the second pair of eyes. I think this might be a problem with:

    http://github.com/josh/regin

    So, I'm going to assign to Josh. My apologies if I'm misguided on this. Thank you!

    June 25th, 2010 @ 05:40 AM

  3. Andrew White
    Andrew White
    • State changed from open to invalid

    You can't use regexp anchors within routing constraints as the whole route is compiled into a larger regexp that begins with \A and may end with \Z (depending on the route spec). This is always been the case even in 2.3.x.

    In your gist the route regexp would be compiled as /\A\/(?:^\d)\Z/ which is a invalid regexp. Regin picks this up which is why it looks as though the problem is there.

    June 25th, 2010 @ 06:56 AM

  4. Trevor Turk
    Trevor Turk

    Well, that's certainly a surprise! I thought that'd be a reasonable constraint.

    Is there any known workaround? In either case, I think this probably warrants some documentation mentioning that it's a known issue. I'm happy to take up the cause on that front. If anyone knows of a workaround, I'd rather include that than a caveat, though. I'll give it a day or two and work up a patch if nobody responds.

    Thanks!

    June 25th, 2010 @ 07:02 AM

  5. Andrew White
    Andrew White

    You don't need the anchor as all routes are anchored at the start. If what you're trying to do is to make sure that if the id param starts with an number then route it to the videos controller then this will work:

      match '/:id' => 'videos#show',
        :constraints => { :id => /\d+(?:-[-_a-zA-Z0-9]+)/ },
        :as => :video
    
      match '/:id' => 'users#show',
        :constraints => { :id => /[a-zA-Z].*/ },
        :as => :user
    

    June 25th, 2010 @ 08:11 AM

  6. Trevor Turk
    Trevor Turk

    Thanks again, Andrew - that really helps.

    I'm adding to Neeraj's commit (thanks, BTW :)

    http://github.com/lifo/docrails/commit/a2242a608e5c3057ec46ddffdd98...

    ...to show an example based on Andrew's suggestion:

    http://github.com/lifo/docrails/commit/81e9627c7f4bad6007a5f8b5b7f5...

    Please feel free to change/remove/improve this, but I think something like it is worth having.

    I also wonder if might be worth providing a nicer error message in this case, if possible, since it's a known issue.

    Thanks!

    June 26th, 2010 @ 12:52 AM