rails 3 在route中重定向

本文介绍了一种使用Rails 3 Route Globbing技术处理未定义路由的方法。通过配置特定的路由规则,可以将所有不存在的URL指向一个统一的错误控制器,并提供自定义的404页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



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'



[url=http://workingwithrails.com/railsplugin/4789-routing-tricks]同行链接[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值