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.

better error messages when incorrect regexp is passed to constraints in routes

#5274

The :constraints parameter in routes expects a regexp without the start/end markers (which is the opposite to e.g. the regexps passed to validation methods in ActiveModel). If you pass a full regexp like /^\w+$/, you get a strange exception message "undefined method include?' for #<Anchor "^">" coming from rack-mount.

It would be more consistent if the regexp style was the same in routes and in ActiveModel, but if that's not possible, then maybe Rails could print more meaningful error messages when incorrect regexp is passed to :constraints instead of that exception.

Reported by Jakub Suder · August 2nd, 2010 @ 05:06 PM

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

Activity

  1. Rohit Arondekar
    Rohit Arondekar
    • State changed from new to invalid
    • Importance changed from to Low

    According to the docs you can use normal regexp's with the start/end markes: http://guides.rubyonrails.org/routing.html#specifying-constraints

    Am I missing something? Maybe this was fixed along the way. I'm closing the ticket, however if this is still an issue just leave a comment with steps to reproduce the problem, or a patch and I'll reopen.

    October 7th, 2010 @ 06:38 AM

  2. Jakub Suder
    Jakub Suder

    Well, the guide you linked to shows examples with regexps without start/end markers...

    Example from that link:

    constraints(:id => /[A-Z][A-Z][0-9]+/) do
    

    And if you do this:

    constraints(:id => /^[A-Z][A-Z][0-9]+$/) do
    

    you get a confusing error and you waste half an hour trying to figure out what may be wrong (it's not just me, someone else from my team had this last week too). This happens on Rails 3.0.0 final, I'm not sure about latest development version.

    October 7th, 2010 @ 07:51 PM

  3. Rohit Arondekar
    Rohit Arondekar
    • State changed from invalid to open
    • Assigned user set to Andrew White

    Sorry for that!

    Well I think I've seen something similar. And iirc, it's not possible/easy to allow use of start/end markers in constraints.

    Assigning to Andrew for clarification.

    October 8th, 2010 @ 02:07 AM

  4. Andrew White
    Andrew White
    • Milestone cleared.

    There's a commit on master which raises an ArgumentError when an anchor is used in a constraint:
    http://github.com/rails/rails/commit/ca3936dbd691538d97d8dbe2bc1d64...

    It's impossible to add anchor support to routing constraints since each regexp is assembled into a larger regexp with it's own anchors. Validations are different because there you are specifying the whole regexp rather than just a fragment so it's perfectly valid that you might want to use an anchor.

    I'll backport the above commit to the 3-0-stable branch - I believe something similar already exists in the 2-3-stable branch.

    October 8th, 2010 @ 07:02 AM

  5. David Trasbo
    David Trasbo
    • State changed from open to resolved

    Fixed on master and will be backported to 3-0-stable.

    October 8th, 2010 @ 08:41 AM

  6. Jakub Suder
    Jakub Suder

    That should be enough, thanks.

    October 8th, 2010 @ 09:23 AM

  7. Repository
  8. Jeremy Kemper
  9. bingbing