<?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>Sean&#039;s Blog &#187; routes</title>
	<atom:link href="http://seanbehan.com/tag/routes/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 18 Jan 2012 21:44:54 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Renaming Routes in Rails 3 with :As and :Controller</title>
		<link>http://seanbehan.com/ruby-on-rails/renaming-routes-in-rails-3-with-as-and-controller/</link>
		<comments>http://seanbehan.com/ruby-on-rails/renaming-routes-in-rails-3-with-as-and-controller/#comments</comments>
		<pubDate>Mon, 13 Sep 2010 02:45:18 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[edge]]></category>
		<category><![CDATA[rails 3]]></category>
		<category><![CDATA[routes]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1183</guid>
		<description><![CDATA[Renaming routes in Rails 2.* is straight forward. It goes something like this. ## config/routes.rb map.resources :users, :as =&#62; :members This will give you users_path and form_for(User.new) helpers, for instance, mapping the url to /members instead of /users while using the users_controller.rb class. In Rails 3.*, this is still possible. It is accomplished differently. In [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Frenaming-routes-in-rails-3-with-as-and-controller%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Renaming Routes in Rails 3 with :As and :Controller" data-url="http://seanbehan.com/ruby-on-rails/renaming-routes-in-rails-3-with-as-and-controller/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>Renaming routes in Rails 2.* is straight forward. It goes something like this. </p>
<pre class="wp-code-highlight prettyprint">
## config/routes.rb
map.resources :users, :as =&gt; :members
</pre>
<p>This will give you users_path and form_for(User.new) helpers, for instance, mapping the url to /members instead of /users while using the users_controller.rb class. </p>
<p>In Rails 3.*, this is still possible. It is accomplished differently. In my opinion, it seems a little less elegant. </p>
<pre class="wp-code-highlight prettyprint">
# config/routes.rb
resources :members, :as =&gt; :users, :controller =&gt; :users
# app/views/**/*.erb
link_to &quot;Members&quot;, users_path
form_for User.new do |f|
# Rake routes
       users GET    /members(.:format)           {:controller=&gt;&quot;users&quot;, :action=&gt;&quot;index&quot;}
       users POST   /members(.:format)           {:controller=&gt;&quot;users&quot;, :action=&gt;&quot;create&quot;}
    new_user GET    /members/new(.:format)       {:controller=&gt;&quot;users&quot;, :action=&gt;&quot;new&quot;}
   edit_user GET    /members/:id/edit(.:format)  {:controller=&gt;&quot;users&quot;, :action=&gt;&quot;edit&quot;}
        user GET    /members/:id(.:format)       {:controller=&gt;&quot;users&quot;, :action=&gt;&quot;show&quot;}
        user PUT    /members/:id(.:format)       {:controller=&gt;&quot;users&quot;, :action=&gt;&quot;update&quot;}
        user DELETE /members/:id(.:format)       {:controller=&gt;&quot;users&quot;, :action=&gt;&quot;destroy&quot;}
</pre>
<p>Your resource is &#8220;members&#8221; and you are overriding the name (using :as=>&#8221;users&#8221;) however, you still have to specify the controller.  </p>
<p><a href="http://pastie.org/1154956">http://pastie.org/1154956</a></p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/renaming-routes-in-rails-3-with-as-and-controller/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Scope Routes/URLs By Username (like Twitter) in Your Rails Application</title>
		<link>http://seanbehan.com/posts/scope-routesurls-by-username-like-twitter-in-your-rails-application/</link>
		<comments>http://seanbehan.com/posts/scope-routesurls-by-username-like-twitter-in-your-rails-application/#comments</comments>
		<pubDate>Sun, 22 Aug 2010 21:09:22 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[link_to]]></category>
		<category><![CDATA[routes]]></category>
		<category><![CDATA[scoping]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[urls]]></category>
		<category><![CDATA[usernames]]></category>
		<category><![CDATA[views]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1135</guid>
		<description><![CDATA[There are a few things that need to be taken care of before you can get this to work. The first thing (although, any of the following steps can be done in any order) to take care of involves your User model. You need to override the to_param method, so that Rails will appropriately use [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fposts%2Fscope-routesurls-by-username-like-twitter-in-your-rails-application%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Scope Routes/URLs By Username (like Twitter) in Your Rails Application" data-url="http://seanbehan.com/posts/scope-routesurls-by-username-like-twitter-in-your-rails-application/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>There are a few things that need to be taken care of before you can get this to work. The first thing (although, any of the following steps can be done in any order) to take care of involves your User model. You need to override the to_param method, so that Rails will appropriately use the username attribute rather than user_id when constructing paths. </p>
<pre class="wp-code-highlight prettyprint">
#in app/models/user.rb
def to_param
  &quot;#{self.username}&quot;
end
</pre>
<p>Next we move onto routing our resources. Here it gets a little tricky because Rails is building paths for us. Remeber, you can get a list of all currently defined routes in your application by running the routes rake task</p>
<pre class="wp-code-highlight prettyprint">
rake routes
</pre>
<p>We need to set the path_prefix option on any of our resources we want scoped by the username. For instance, in this example, I have set up a Status model and statuses_controller, whose urls shall be scoped by the username. You can apply the path_prefix to any number of other resources in your routes config file. They symbol used is arbitrary, but will be made available in the params hash,  in this case params[:user_id]. You also need to exclude the show action on your users resources declaration. The reason is that, otherwise, Rails will include the controller name in the path like /users/username, which doesn&#8217;t look as clean as just /username. You then need to redefine this route explicitly (last line in the routes config shown here). </p>
<pre class="wp-code-highlight prettyprint">
#in app/config/routes.rb
map.resources :statuses,   :path_prefix =&amp;gt; '/:user_id'
map.resources :users,     :except =&amp;gt; [:show]
map.user '/:username' :controller =&amp;gt; 'users', :action =&amp;gt; 'show'
</pre>
<p>Finally, you get to call these routes in your views or use them in your controllers. You use the same link_to, url_for methods to generate paths. When constructing the resources you have setup with the path_prefix declartion, remember you need the user model as the first argument, followed by said resource. </p>
<pre class="wp-code-highlight prettyprint">
&amp;lt;%= link_to(status.title, status_path(status.user,status) %&amp;gt;
# or in a controller
redirect_to status_path( status.user, status )
</pre>
<p>That&#8217;s pretty much it. If anyone has another way of doing this let me know! </p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/posts/scope-routesurls-by-username-like-twitter-in-your-rails-application/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How to Use Pretty URLs with Rails will_paginate Plugin</title>
		<link>http://seanbehan.com/ruby-on-rails/how-to-use-pretty-urls-with-rails-will_paginate-plugin/</link>
		<comments>http://seanbehan.com/ruby-on-rails/how-to-use-pretty-urls-with-rails-will_paginate-plugin/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 01:43:54 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[caching]]></category>
		<category><![CDATA[routes]]></category>
		<category><![CDATA[urls]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=321</guid>
		<description><![CDATA[The will_paginate plugin for Rails uses a key/value assignment like ?page=2, rather than the pretty url formats such as /page/2 &#8230; This is because url generation and mapping are handled by the routes.rb file. You&#8217;ll need to modify the file so that rails knows what to do with request that match the pattern. Make sure [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fhow-to-use-pretty-urls-with-rails-will_paginate-plugin%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="How to Use Pretty URLs with Rails will_paginate Plugin" data-url="http://seanbehan.com/ruby-on-rails/how-to-use-pretty-urls-with-rails-will_paginate-plugin/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>The will_paginate plugin for Rails uses a key/value assignment like ?page=2, rather than the pretty url formats such as /page/2 &#8230; This is because url generation and mapping are handled by the routes.rb file. You&#8217;ll need to modify the file so that rails knows what to do with request that match the pattern. Make sure to put the custom (map.connect) route before the normal restful routes (map.resources)</p>
<pre class="wp-code-highlight prettyprint">
map.connect '/topics/:id/page/:page', :controller =&gt; 'topics', :action =&gt; 'show'
map.resources :topics
</pre>
<p>Internally will_paginate uses the url_for method so Rails will now know how to construct your urls in a pretty way. I got most of this info from this discusson http://groups.google.com/group/will_paginate/browse_thread/thread/d0142b512cfca9d5?pli=1</p>
<p>There is nothing wrong leaving the default behavior alone and looking at the &#8216;ugly&#8217; key/value pairs in the address bar. However, if you take advantage of page caching in rails, you&#8217;ll need to do this anyway. Page caching in rails ignore extra parameters, any info before/after the &#8220;?&#8221; and &#8220;&#038;&#8221; symbols. There will be no difference from /topics/2?page=1 and /topics/2?page=100 in your cache. And since most likely the content will be very different on these two pages, you&#8217;ll need to have pretty urls so that page caching will save topics/2/page/1.html and topics/2/page/100.html as two different resources!</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/how-to-use-pretty-urls-with-rails-will_paginate-plugin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Namespacing in Rails</title>
		<link>http://seanbehan.com/ruby-on-rails/namespacing-in-rails/</link>
		<comments>http://seanbehan.com/ruby-on-rails/namespacing-in-rails/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 19:36:50 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[grep]]></category>
		<category><![CDATA[namespace]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[routes]]></category>

		<guid isPermaLink="false">http://bseanvt.wordpress.com/?p=85</guid>
		<description><![CDATA[With namespace routes in Rails you can easily create a prefix for select resources. For instance, if you have an admin area or an account dashboard you can give logged in users access to methods that are not otherwise available. To use namespaces define them in config/routes.rb. *Note: this is available for the latest release [...]]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:px;">
				<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fseanbehan.com%2Fruby-on-rails%2Fnamespacing-in-rails%2F&amp;layout=button_count&amp;show_faces=false&amp;width=&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:px; height:27px;" allowTransparency="true"></iframe>
				</div><div class="really_simple_share_twitter" style="width:px;">
					<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Namespacing in Rails" data-url="http://seanbehan.com/ruby-on-rails/namespacing-in-rails/" 
						data-via="" ></a> 
				</div></div>
		<div style="clear:both;"></div><p>With namespace routes in Rails you can easily create a prefix for select resources. For instance, if you have an admin area or an account dashboard you can give logged in users access to methods that are not otherwise available. To use namespaces define them in config/routes.rb. *Note: this is available for the latest release of Rails, at this time it is 2.3.0. I think it&#8217;s supported back until Rails 2.0, but haven&#8217;t tested it.</p>
<p>In your config/routes.rb file</p>
<pre class="wp-code-highlight prettyprint">map.namespace :dashboard do |dashboard|
    dashboard.resources :properties, :collection=&amp;gt;{:available =&amp;gt; :get}
end</pre>
<p>This namespace will create routes such as dashboard/properties, dashboard/properties/1, essentially all the CRUD paths as well as a collection method &#8220;available&#8221; dashboard/properties/available. These routes can be accessed in your views with methods such as new_dashboard_property_path, available_dashboard_properties_path  etc. If your property model has it&#8217;s own controller that isn&#8217;t namespaced, properties_controller.rb, remember to modify the property form_for method to tell it to use the dashboard namespace. The normal form_for(@property), method takes just your property object. Instead pass the dashboard symbol inside of an array  to form_for</p>
<pre class="wp-code-highlight prettyprint">&amp;lt;% form_for [:dashboard, @property] do |f| %&amp;gt;&amp;lt;% end %&amp;gt;</pre>
<p>You can easily see all of the available paths from the command line using rake routes. I use grep to filter the results for conveinence, because on a large application I don&#8217;t necessarily need to see all the routes if I&#8217;m working just within a single namespace. rake routes | grep dashboard</p>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/namespacing-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

