Route and patterns

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.

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

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/ 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:

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:

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}):