<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: routes code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 08 Aug 2008 15:51:19 GMT</pubDate>
    <description>DZone Snippets: routes code</description>
    <item>
      <title>Make your model play nice with SEO</title>
      <link>http://snippets.dzone.com/posts/show/4967</link>
      <description>&lt;code&gt;&lt;br /&gt;## config/initializers/string_extensions.rb&lt;br /&gt;&lt;br /&gt;require 'unicode'&lt;br /&gt;&lt;br /&gt;class String&lt;br /&gt;  def to_slug&lt;br /&gt;    str = Unicode.normalize_KD(self).gsub(/[^\x00-\x7F]/n,'')&lt;br /&gt;    str = str.gsub(/\W+/, '-').gsub(/^-+/,'').gsub(/-+$/,'').downcase&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def numeric?&lt;br /&gt;    if self =~ /^\d+$/&lt;br /&gt;      self.to_i&lt;br /&gt;    elsif self =~ /^\d+([,\.]\d+)?$/&lt;br /&gt;      self.tr(',','.').to_f&lt;br /&gt;    else&lt;br /&gt;      false&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;## app/models/article.rb&lt;br /&gt;&lt;br /&gt;# string :title&lt;br /&gt;# text   :body&lt;br /&gt;# string :permalink&lt;br /&gt;class Article &lt; ActiveRecord::Base&lt;br /&gt;  before_save :set_permalink&lt;br /&gt;&lt;br /&gt;  # article_path(@article)&lt;br /&gt;  # &gt;&gt; /article/5&lt;br /&gt;  # article_path(@article.permalink)&lt;br /&gt;  # /article/pink-ferret&lt;br /&gt;  def self.find(*args)&lt;br /&gt;    if args.first.is_a?(String) and !args.first.numeric?&lt;br /&gt;      find_by_permalink(args.shift,*args) or raise ActiveRecord::RecordNotFound&lt;br /&gt;    else&lt;br /&gt;      super&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  # article_path(@article)&lt;br /&gt;  # &gt;&gt; /article/5-pink-ferret&lt;br /&gt;  def to_param&lt;br /&gt;    "#{id}-#{permalink}"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  private&lt;br /&gt;  def set_permalink&lt;br /&gt;    self.permalink = title.to_slug&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 09 Jan 2008 15:13:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4967</guid>
      <author>jerome ()</author>
    </item>
    <item>
      <title>Rails route lets :id be a domain name</title>
      <link>http://snippets.dzone.com/posts/show/4376</link>
      <description>// Named route for an id that contains . characters, such as a domain name&lt;br /&gt;// eg: http://myapp/domains/foobar.com&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;map.domain 'domains/:id', :controller =&gt; 'domains', :action =&gt; 'show', :requirements =&gt; { :id =&gt; /[a-zA-Z0-9\-\.]+/ }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 01 Aug 2007 02:51:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4376</guid>
      <author>logankoester (Logan Koester)</author>
    </item>
    <item>
      <title>exception_logger route</title>
      <link>http://snippets.dzone.com/posts/show/3330</link>
      <description>&lt;code&gt;&lt;br /&gt;map.exceptions '/logged_exceptions/:action/:id', :controller =&gt; 'logged_exceptions', :action =&gt; 'index', :id =&gt; nil &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 19 Jan 2007 06:16:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3330</guid>
      <author>caffo ()</author>
    </item>
    <item>
      <title>Named Routing in Rails (getting rid of the ids)</title>
      <link>http://snippets.dzone.com/posts/show/1587</link>
      <description>Lifted from Semergence&lt;br /&gt;(http://www.semergence.com/archives/2005/12/01/10/02/32/)&lt;br /&gt;&lt;br /&gt;In config/routes.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;map.project &#8216;project/:projtitle&#8217; :controller =&gt; &#8216;project&#8217;, :action =&gt; &#8217;show&#8217;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In app/controllers/project_controller.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;   def show&lt;br /&gt;     @project = Project.find(:first,&lt;br /&gt;                             :conditions =&gt; [&#8217;title = ?&#8217;,params[:projtitle]])&lt;br /&gt;     &#8230;&lt;br /&gt;   end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Or something like that. Assuming your project titles are unique. Then&lt;br /&gt;you can also use project_url (since you have map.project in routes.rb)&lt;br /&gt;for urls in your views, e.g.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;   &lt;% for project in @projects %&gt;&lt;br /&gt;   &lt;%= link_to project.title,&lt;br /&gt;               project_url(:projtitle =&gt; project.title) %&gt;&lt;br /&gt;    &#8230;&lt;br /&gt;   &lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 27 Feb 2006 20:04:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1587</guid>
      <author>monkeymagic ()</author>
    </item>
    <item>
      <title>Alternative Default Routes</title>
      <link>http://snippets.dzone.com/posts/show/766</link>
      <description>I prefer :controller/:id/:action formatted URIs to :controller/:action/:id. For instance, "people/56/edit" to "people/edit/56". This is a simple matter of taste and has no real benefits other than being a bit more intuitive to certain brains. &lt;br /&gt;&lt;br /&gt;It's also a bit harder to do than you might expect. You need to replace the default :controller/:action/:id route with the following:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;map.connect ':controller/:action', &lt;br /&gt;            :requirements =&gt; { :action =&gt; /\D+/ }&lt;br /&gt;&lt;br /&gt;map.connect ':controller/:id/:action',&lt;br /&gt;            :action =&gt; 'show',&lt;br /&gt;            :requirements =&gt; { :id =&gt; /\d+/ }&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This will result in the following URIs for the basic set of CRUD actions generated by default:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/people/         (:action =&gt; 'index')&lt;br /&gt;/people/new&lt;br /&gt;/people/create&lt;br /&gt;/people/56/      (:action =&gt; 'show' by default)&lt;br /&gt;/people/56/edit&lt;br /&gt;/people/56/remove&lt;br /&gt;/people/56/update&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You could argue that the default route setup in Rails is a correct default because it's a bit more simple to apply across all cases.&lt;br /&gt;</description>
      <pubDate>Wed, 28 Sep 2005 00:18:49 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/766</guid>
      <author>rtomayko (Ryan Tomayko)</author>
    </item>
    <item>
      <title>Create spiffy myhost/[keyword] type routing</title>
      <link>http://snippets.dzone.com/posts/show/536</link>
      <description>Except that I can't figure out how to do it in routes.  This is what I found.&lt;br /&gt;&lt;br /&gt;In *config/routes/rb*, as the last route add:&lt;br /&gt;&lt;code&gt;&lt;br /&gt; map.connect '*path', :controller =&gt; 'application', :action =&gt; 'handle_unrecog'&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;In *app/controllers/application.rb*, put this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt; def handle_unrecog&lt;br /&gt;  #do something here, the path info is in the @params value&lt;br /&gt; end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;One of my uses for this is for a del.icio.us type user viewing.  IE: _http://del.icio.us/[user]_ but having it only valid for users.&lt;br /&gt;&lt;br /&gt;I'd do something like this:&lt;br /&gt;&lt;code&gt;&lt;br /&gt; def handle_unrec&lt;br /&gt;  uname = @params['path'][0] #first part of the path&lt;br /&gt;  user = User.find_by_login( uname )&lt;br /&gt;  if user&lt;br /&gt;   return render :controller =&gt; "user", :action =&gt; "show", :id =&gt; user&lt;br /&gt;  else&lt;br /&gt;   return render :controller =&gt; "user", :action =&gt; "notfound"&lt;br /&gt;  end&lt;br /&gt; end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Yay!</description>
      <pubDate>Tue, 02 Aug 2005 05:24:57 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/536</guid>
      <author>jmoses (jon)</author>
    </item>
    <item>
      <title>Snippets' routes</title>
      <link>http://snippets.dzone.com/posts/show/64</link>
      <description>These routes allow URLs like tags/whatever/whatever/whatever to put 'whatever', 'whatever', and 'whatever' into the @params[:tags] array, etc.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;map.connect '', :controller =&gt; "posts", :action =&gt; "index"&lt;br /&gt;map.connect 'tags/*tags', :controller =&gt; 'tag', :action =&gt; 'show'&lt;br /&gt;map.connect 'user/:user', :controller =&gt; 'user', :action =&gt; 'show'&lt;br /&gt;map.connect 'user/:user/tags/*tags', :controller =&gt; 'tag', :action =&gt; 'show'&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;</description>
      <pubDate>Fri, 08 Apr 2005 01:46:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/64</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
    <item>
      <title>Pretty archive URLs in Typo with Routes</title>
      <link>http://snippets.dzone.com/posts/show/31</link>
      <description>Tobi posted this as a new URL scheme for Typo.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;# allow neat perma urls&lt;br /&gt;map.connect 'articles/:year/:month/:day', :controller  =&gt; 'articles', &lt;br /&gt;     :action =&gt; 'find_by_date', &lt;br /&gt;     :year =&gt; /\d{4}/, :day =&gt; nil, :month =&gt; nil&lt;br /&gt;map.connect 'articles/:year/:month/:day/:title', :controller  =&gt; 'articles', &lt;br /&gt;     :action =&gt; 'permalink', :year =&gt; /\d{4}/&lt;/code&gt;</description>
      <pubDate>Sun, 03 Apr 2005 16:41:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/31</guid>
      <author>peter (Peter Cooperx)</author>
    </item>
  </channel>
</rss>
