views:

860

answers:

3

Is it possible to do a redirect in the routes file of a Rails app?

Specifically, I'd like to forward /j/e to /javascripts/embed.js

Right now the only way I can think to do it is to create a j controller with an e method that redirects to that.

+1  A: 

Assuming rails version prior to 3.

You can create a new RedirectController or tuck a single function away in an existing controller to do something like the following:

map.js_embed '/j/e',
    :controller => :redirect_controller,
    :action => :some_function,
    :path => "embed"

Then your function would do this:

def some_function
  if params[:path]
    redirect_to "/javascripts/#{params[:path]}.js"
  end
end

Or something to that effect.

Tony Fontenot
+1  A: 

The Routing Tricks plugin enables you to define redirects in the routing file. Works well for me.

Larry K
+2  A: 

New with Rails 3, you can redirect inside the routes.rb file.

match "/posts/github" => redirect("http://github.com/rails.atom")
Steven Soroka