---
title: "rails 3 在route中重定向"
author: "夜鸣猪"
date: "2012-09-02T09:10:01Z"
tags: []
categories: ["编程语言"]
slug: "rails-3-route"
draft: false
lastmod: "2012-09-02T09:10:01Z"
id: 1669624
url: "https://hlee.iteye.com/blog/1669624"
---

```ruby


Yourapp::Application.routes.draw do
  #Last route in routes.rb
  match '*a', :to => 'errors#routing'
end
NOTE: The "a" is actually a parameter in the Rails 3 Route Globbing technique. For example, if your url was /this-url-does-not-exist, then params[:a] equals "/this-url-does-not-exist". So be as creative as you'd like handling that rogue route.

app/controllers/errors_controller.rb

Here, I handle my routing errors. I leverage previous 404 handling code from my original ApplicationController mentioned above. So, my errors_controller.rb looks like this:

class ErrorsController < ApplicationController
  def routing
    render_404
  end
end
However, feel free to modify to fit your individual needs. Everyone's situation will be slightly different. For example, if you're not going to reuse your 404 error handling logic, then here's the full ErrorsController without inheritance:

class ErrorsController < ApplicationController
  def routing
   render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
  end
end

```




```ruby

match "/posts/github" => redirect("http://github.com/rails.atom")
match '*path' => redirect('/')   unless Rails.env.development?

match ':graphs/:id(/:method)' => 'pages#something'

```



[同行链接](http://workingwithrails.com/railsplugin/4789-routing-tricks)