Harue
Harue

Reputation: 149

Routing with regex condition in Rails 2

In my routes.rb file, I've defined these routes:

  map.with_options(:controller => "review") do |review|
    review.review_index    "/review/:page", :action => "index", :defaults => {:page => nil}, :requirements => {:page => /[0-9]./}
    review.review_provider "/review/:category_name/:page", :action => "provider", :defaults => {:page => nil}
  end

However, it only match with the second routes. For example,

/review/1

must match with first rule but in fact it is matched with the second rule.

How can I config it so that:

/review/1 will match with the first rule
/review/a_category/1 will  match with the second rule

Upvotes: 0

Views: 109

Answers (1)

Nick Messick
Nick Messick

Reputation: 3212

Your regular expression in your first route is bad. Period matches any single character.

/[0-9]/ means "any number, followed by any other single character".

So, that would match /review/1a, /review/70, /review/7?, etc,

If you want to match one or more digits, change your regular expression to: /[0-9]+/

Upvotes: 1

Related Questions