# Route and patterns

**URL:** <https://www.ruby-forum.com/t/route-and-patterns/122794>\
**Category:** Rails\
**Created:** [December 12, 2007, 7:04am UTC](https://www.ruby-forum.com/t/route-and-patterns/122794 "2007-12-12T07:04:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 12, 2007, 7:04am UTC](https://www.ruby-forum.com/t/route-and-patterns/122794/1 "2007-12-12T07:04:35Z")

</div>

In a wiki, routes are handling

web\_index /:web {:controller=\>“wiki”, :action=\>“index”}  
web\_topic /:web/:topic {:controller=\>“wiki”, :action=\>“view”}  
action /:web/:topic/:action {:controller=\>“wiki”}

That is the default action is “view”.

Two questions:

1. Can I constrain the patterns allowed for “:web” and “:topic”

2. Can I use that for something like

webtopic /:web.:topic {:controller=\>“wiki”, :action=\>“view”}

Obviously part of the constraint is that neither part can contain a  
period.

–  
TCP/IP Illustrated… Is that the swimsuit edition? – rgh22

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 12, 2007, 10:53am UTC](https://www.ruby-forum.com/t/route-and-patterns/122794/2 "2007-12-12T10:53:01Z")

</div>

The answer to the first question is yes.

You can do something like this:

map.date “blog/:year/:month/:day”,  
:controller =\> “blog”,  
:action =\> “show\_date”,  
:requirements =\> { :year =\> /(19|20)\d\d/, :month =\> /[01]?\d/, :day  
=\> /[0-3]?\d/ },  
:day =\> nil,  
:month =\> nil

(Page 404 of Agile Web D. with Rails 2nd Ed)

If :web and :topic are user created, you should probably also use  
active record validations validates\_format\_of …

The answer to question 2 is I think so since the standard routes  
include .:format

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 12, 2007, 9:47pm UTC](https://www.ruby-forum.com/t/route-and-patterns/122794/3 "2007-12-12T21:47:25Z")

</div>

Is this something that varies with the version of Rials,because I’m  
having problems with it.

I have in config/routes.rb:

map.with\_options(:controller =\> ‘wiki’) do |wiki|  
wiki.web ‘:web’, :action =\> ‘view’,  
:topic =\> ‘HomePage’,  
:requirements =\> {  
:web =\> %r{[A-Z][a-z]+}

```
                                     }

wiki.web_topic ':web/:topic', :action => 'view',
                                 :requirements => {
                                   :web => %r{[A-Z][a-z]+},
                                   :topic => %r{([A-Z][a-z])+{2,}}
                                  }

# Everything else
wiki.connect '*url', :action => 'view_by_url'

```

end

Before I added the ‘requirements’ the following were caught correctly  
/System  
/System/CamelWord

After I added the ‘requirements’ everything ended up as ‘view\_by\_url’

I’ve simplified the patterns all the way down, but I think they are  
right. I’ve tested at [http://regexlib.com/](http://regexlib.com/)  
I’ve tried more “complicated” patterns: start of word/string markers,  
explicit quantifiers, groupings, greedy and ungreedy. No avail.  
I always end up with the ‘view\_by\_url’ catch-all.

I’m don’t see how “:format” is relevant. Could you expand on tat  
please.

ptontiger said the following on 11/12/07 07:34 PM:

> :month =\> nil
> 
> > In a wiki, routes are handling
> > 
> > 1. Can I use that for something like
> > 
> > webtopic /:web.:topic {:controller=\>“wiki”, :action=\>“view”}
> > 
> > Obviously part of the constraint is that neither part can contain a period.

–  
“It’s better to light a candle than to curse the darkness.”  
- Eleanor Roosevelt

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 15, 2007, 10:38pm UTC](https://www.ruby-forum.com/t/route-and-patterns/122794/4 "2007-12-15T22:38:00Z")

</div>

I have part of that working.  
This part:

wiki.web\_topic ‘:web/:topic’, :action =\> ‘view’,  
:web =\> nil,  
:topic =\> nil,  
:requirements =\> {  
:web =\> /[A-Z][a-z]+/,  
:topic =\> /([A-Z][a-z])+{2,}/,  
}

I do need to make the regular expressions more comprehensive, though.

But if extend it to:

wiki.web\_topic\_action ‘:web/:topic/:action’,  
:requirements =\> {  
:web =\> /[A-Z][a-z]+/,  
:topic =\> /([A-Z][a-z])+{2,}/,  
:action =\> /[a-z][a-z\_]{3,}/  
}

I don’t get matches. I also don’t get matches on a simpler, easier to  
test variation:

wiki.web\_action ‘:web/:action’,  
:requirements =\> {  
:web =\> /[A-Z][a-z]+/,  
:action =\> /[a-z][a-z\_]{3,}/  
}

I take it that the "nil’ makes the field optional and if there is no  
‘nil’ then its mandatory. Is there a way to supply a default?  
If so, does it have to be a fixed string or can I pull it out of a  
configuration table in the database?

ptontiger said the following on 11/12/07 07:34 PM:

> :month =\> nil
> 
> > In a wiki, routes are handling
> > 
> > 1. Can I use that for something like
> > 
> > webtopic /:web.:topic {:controller=\>“wiki”, :action=\>“view”}
> > 
> > Obviously part of the constraint is that neither part can contain a period.  
> > –  
> > When I used to teach creative writing, I would tell the students to make  
> > their characters want something right away even if it’s only a glass of  
> > water. Characters paralyzed by the meaninglessness of modern life still  
> > have to drink water from time to time.  
> > –Kurt Vonnegut

---

<div class="post-metadata">

**Author:** ![antonaylward](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eb8c5e/32.png) [@antonaylward](https://www.ruby-forum.com/u/antonaylward)\
**Post date:** [December 15, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/route-and-patterns/122794/5 "2007-12-15T22:53:13Z")

</div>

Anton J Aylward said the following on 15/12/07 04:37 PM:

> I have part of that working.  
> This part:
> 
> wiki.web\_topic ‘:web/:topic’, :action =\> ‘view’,  
> :web =\> nil,  
> :topic =\> nil,  
> :requirements =\> {  
> :web =\> /[A-Z][a-z]+/,  
> :topic =\> /([A-Z][a-z])+{2,}/,  
> }

WHOOP!  
Let me take that back. No it doesn’t work.

ActionController::RoutingError (No route matches “/System/HomePage” with  
{:method=\>:get}):

–  
Hell must be like a karaoke bar

- removed\_email\_address@domain.invalid
