rails 3 在route中重定向_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > rails 3 在route中重定向

rails 3 在route中重定向

 2012/9/8 11:52:54  夜鸣猪  程序员俱乐部  我要评论(0)
  • 摘要:Yourapp::Application.routes.drawdo#Lastrouteinroutes.rbmatch'*a',:to=>'errors#routing'endNOTE:The"a"isactuallyaparameterintheRails3RouteGlobbingtechnique.Forexample,ifyoururlwas/this-url-does-not-exist,thenparams[:a]equals"/this-url-does-not
  • 标签:rails

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'



同行链接
发表评论
用户名: 匿名