Zeck
Zeck

Reputation: 6579

Rails Routes: Need regex for permalink

I have a following routes in my routes.rb

constraints(category: REGEX_PERMALINK, course: REGEX_PERMALINK, lecture: REGEX_PERMALINK) do
  get '/:category', to: 'notes#index'
  get '/:category/:course', to: 'notes#index'
  get '/:category/:course/:lecture', to: 'notes#index'
end

The REGEX_PERMALINK is /\d.+/. When I go to localhost:3001/admin, :category is set to admin. It's wrong and I'm not going to admin. :category, :course, :lecture are only start with id then slug /permalink/. For example: localhost:3001/1-foo/2-bar/3-me should category: 1-foo, course: 2-bar, lecture: 3-me. Can you guys write a regex for REGEX_PERMALINK.

Thank you for help :D

Upvotes: 1

Views: 199

Answers (1)

Trupti Jangam
Trupti Jangam

Reputation: 96

Try following RegEx

^\d-\w+

This means, expression starting with digit followed by dash and \w will match to any word character (letter, number, underscore). And + means one or more time.

Upvotes: 2

Related Questions