<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Floyd's Thoughts... &#187; Routing</title>
	<atom:link href="http://www.floydprice.com/category/routing/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.floydprice.com</link>
	<description>Because Everything is Interesting!!!</description>
	<lastBuildDate>Fri, 18 Jun 2010 05:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Catch-All routes in Rails</title>
		<link>http://www.floydprice.com/2008/08/catch-all-routes-in-rails/</link>
		<comments>http://www.floydprice.com/2008/08/catch-all-routes-in-rails/#comments</comments>
		<pubDate>Tue, 19 Aug 2008 15:36:25 +0000</pubDate>
		<dc:creator>Floyd Price</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Rails]]></category>
		<category><![CDATA[Routing]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.floydprice.com/?p=78</guid>
		<description><![CDATA[Sometimes you need a catch-all route in rails to support dynamic applications like Content Management Systems, where all requests that are not matched by an existing route get passed to a controller who can deal with the request. Add the following to the end of your routes.rb : map.with_options(:controller => &#8216;page_engine&#8217;) do &#124;site&#124; site.connect &#8216;*url&#8217;, [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you need a catch-all route in rails to support dynamic applications like Content Management Systems, where all requests that are not matched by an existing route get passed to a controller who can deal with the request.</p>
<p>Add the following to the end of your routes.rb :</p>
<p>map.with_options(:controller => &#8216;page_engine&#8217;) do |site|<br />
  site.connect &#8216;*url&#8217;, :action => &#8216;show_page&#8217;<br />
end</p>
<p>If that’s your last route, it means that anything that isn’t recognised by any of the other routes will get routed to that controller/action (page_engine/show_page). It shouldn’t interfere with images/assets because they are served with higher priority than Rails routes. It lets you have any number of forward-slashes. </p>
<p>So if you hit your site with the following URL:</p>

<div class="wp_syntax"><div class="code"><pre class="url" style="font-family:monospace;">http://localhost:3000/we/really/hate/wcf</pre></div></div>

<p>The request will be routed to the show_page action in the page_engine controller where you can then access the actual url elements with :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;">path = request.<span style="color:#9900CC;">path</span> <span style="color:#008000; font-style:italic;"># '/we/really/hate/wcf'</span>
path_elements = request.<span style="color:#9900CC;">path</span>.<span style="color:#CC0066; font-weight:bold;">split</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'/'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># ['', 'we', 'really', 'hate', 'wcf']</span></pre></td></tr></table></div>

<p>While this isn&#8217;t the type of thing you would do on many Rails projects you certainly will find this useful for projects that have an element of dynamic routing that goes beyond the RESTful style that rails implements, and its especially useful for implementing Content Management Systems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.floydprice.com/2008/08/catch-all-routes-in-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
