This project is archived and is in readonly mode.

#4963 ✓invalid
Trevor Turk

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

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

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...

Comments and changes to this ticket

  • Neeraj Singh

    Neeraj Singh June 25th, 2010 @ 05:19 AM

    • State changed from “new” to “open”

    I am able to reproduce this case with rails edge.

  • Trevor Turk

    Trevor Turk June 25th, 2010 @ 05:40 AM

    • 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!

  • Andrew White

    Andrew White June 25th, 2010 @ 06:56 AM

    • 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.

  • Trevor Turk

    Trevor Turk June 25th, 2010 @ 07:02 AM

    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!

  • Andrew White

    Andrew White June 25th, 2010 @ 08:11 AM

    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
    
  • Trevor Turk

    Trevor Turk June 26th, 2010 @ 12:52 AM

    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!

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Pages