Renaming a controller and redirection in Rails 3
Thu Sep 30 18:27:00 -0700 2010
I had the situation where I wanted to rename a basic part of the TellThemWhen website, that is, changing the name of “instants” to “notifications”. As this was a major part of the whole website, I had to make sure existing URLs still resolved correctly, luckily in Rails 3, this couldn’t be simpler!
This was a fairly big issue, “Instants” were the name of the basic product of the site, that is, you could make an “instant” to share with other people. After a lot of varied feedback however, we decided that “notification” was a better name.
But all the URLs our clients have been distributing look like http://tellthemwhen.com/instants/a3s2… and people have been landing on our new instant page directly per our analytics data.
So I needed a solution which basically remapped /instants/ANYTHING to /notifications/ANYTHING. Happily, the Rails 3 router solves this.
First step is to make a redirector. This is a really simple rack app, I made mine like so and just put it at the top of my config/routes.rb file, it looks like this:
module TellThemWhen
class InstantRedirector
def self.call(env)
destination = "#{env['PATH_INFO']}".sub('instant', 'notification')
destination << "?#{env['QUERY_STRING']}" unless env['QUERY_STRING'].empty?
[301, {'Location' => destination}, ['Instants are now called notifications']]
end
end
end
TellThemWhen::Application.routes.draw do
# ...
end
This is a simple Rack application. It has a class method called “call” that the current environment as an attribute, then pulls the path data substituting the first occurrence of “instant” with “notification”, then adds all of the params data on the end, and sends back a 301 redirect to the client with this new URL
Then in the routes.rb I have:
TellThemWhen::Application.routes.draw do # ... # Catch old instant style URL and redirect to Notifications match '(/my)/instant(s)(/:id)' => TellThemWhen::InstantRedirector end
Here I need to match both “/my/instant” and “/instant” as well as the plural form of instants, I also want to be sure I am only matching the URLs that have instant directly off the root, just in case in the future I have a URL that looks like “/notification/instant”.
Using the parentheses around (/my) and (s) and (/:id) make these optional to the match process.
This match statement simply calls the rack app, InstantRedirector which then runs the call class method, returning the redirection array sending the user back to the correct notifications path with a 301 permanent redirect.
Sweet.
blogLater
Mikel




Tue Nov 08 19:31:33 -0800 2011
I had the same situation as yours and haven’t know what to do. Thanks a lot for the help.
Sat Aug 27 02:06:47 -0700 2011
Hi,
I have read several of your tips. However I came through a stackoverflow question on betting user ip in production with apache and mongreal, although I only use passenger. To the point: a simple search function for a topics would make your site much more usable.
Br
Rutger
Fri Oct 07 00:40:56 -0700 2011
I had the same situation as yours and haven’t know what to do. Thanks a lot for the help.
Sun Feb 05 20:17:51 -0800 2012
I have read several of your tips. However I came through a stackoverflow question on betting user ip in production with apache and mongreal, although I only use passenger. To the point: a simple search function for a topics would make your site much more usable.
Tue Feb 21 08:01:48 -0800 2012
I will use the above tips for my future projects. I am sure that I can have excellent results.
Tue Feb 28 06:45:54 -0800 2012
simple search function for a topics would make your site much more usable.
Mon Mar 12 02:47:07 -0700 2012
I understand how to do it and I will try to apply your advice on my projects.
Wed Mar 14 07:20:19 -0700 2012
why should I do it? I mean for long term. Where can we see updates?
Wed Mar 28 06:29:06 -0700 2012
I will do it like you said. I am sure that I will have good results.
Fri Apr 20 05:29:44 -0700 2012
I have read several of your tips. However I came through a stackoverflow question on betting user ip in production with apache and mongreal, although I only use passenger.