ITeye博客

rails 3 在route中重定向

夜鸣猪 · 2012-09-02 09:10 · Ruby On Rails · 编程语言 · Markdown



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


match "/posts/github" => redirect("http://github.com/rails.atom")
match '*path' => redirect('/')   unless Rails.env.development?

match ':graphs/:id(/:method)' => 'pages#something'

同行链接

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。