1

We are building a Rails CMS where a blog or news listing can appear anywhere in the site tree. This means that any page knows their type based on a database field - eg: a page knows that it is of type newslisting so once that URL is called for that page we need it to be processed by the newslistingcontroller. The subpages of the newslisting page are news stories - each with a date and category.

The URL to the newslisting might be

/dogs/snoopy-news/ 

and a news story might be

/dogs/snoopy-news/snoopy-is-great/

The complexity for us is with URLs that have additional parameters in for listing date ranges of news stories or category listings eg:

Listing all november news stories

/dogs/snoopy-news/2010/11/

Or listing all stories which have food as their category:

/dogs/snoopy-news/category/food/

Since that newslisting page could be at any level in the site hierarchy how would we approach identifying that 2010 is a parameter or that category is a parameter?

(We will store all URLs in the database for each page so we can lookup first to see if a page exists for a URL)

EDIT: This post gives a possible solution: Dynamic CMS like routes in ruby on rails which we will try out and report back on. On save of a page in the CMS we would add routes to the routing table at that point for not only the page itself but for also possible parameters:

/dogs/snoopy-news/{year}/{month}/
Community
  • 1
  • 1
petenelson
  • 460
  • 2
  • 15

2 Answers2

2

You can use route globbing and constraints to match a proper pattern.

# Rails 2.x
map.connect "*path/:year/:month", 
             :constraints => {:year => /\d{4}/, :month => /0[1-9]|1[0-2]/ },
             :controller => :pages, :action => :month_archive

# Rails 3.x
match "*path/:year/:month" => "pages#month_archive", 
             :constraints => {:year => /\d{4}/, :month => /0[1-9]|1[0-2]/ }

This will match /dogs/snoopy-news/2010/11 and pass :path => "dogs/snoopy-news", :year => "2010", :month => "11" in the params hash. It will match all routes that have a year and a month as the last two segments, regardless of how many segments come beforehand. And it will reject any route that doesn't match a proper year and month in the last two segments. What you do with the :path parameter is up to you in the controller.

edgerunner
  • 14,873
  • 2
  • 57
  • 69
1

You can configure this issue in your routes.rb file.

you can add a route like

 map.connect 'snoopy-news/:year/:date', :controller => 'needed controller', :action => 'needed action' 

this will route any URL of the format

 ../snoopy-news/2010/23 
to the corresponding controller and action with the values set in the varialbles
 year , date 

If you dont give anything, RAILS will consider this as a parameter.

CHID
  • 1,625
  • 4
  • 24
  • 43
  • Thanks for the answer but this will not work because /snoopy-news/ is a dynamic page in the website and could appear at any level in the hierarchy. If the user edits this page in the cms and calls it /snoopy-special-news/ this will then not work. The only possible solution I can think of (and don't know how to implement) is when that page is saved, save a route dynamically to achieve this. Would this work or is their a better, simpler approach? – petenelson Jan 14 '11 at 15:53
  • May be u can receive the url `dynamic url` in a default action. Get the parameters and use a comparison to dynamically cal the url will help?? am not pretty sure with this idea. Since u say that the page is completely dynamic we cannot produce any static route. – CHID Jan 14 '11 at 17:18
  • saving the route will be on the expensive side – CHID Jan 14 '11 at 17:20