我看到了源码里 routes.rb 文件里有这样一句:
match '*path', via: :all, to: 'home#error_404'
我把这句也加到了我的项目里,只是我改成了"match '*path'=>'welcome#error_404', via: :all",然后我的路由是这样的:
Prefix Verb URI Pattern Controller#Action
ru_captcha /rucaptcha RuCaptcha::Engine
new_user_session GET /users/sign_in(.:format) sessions#new
user_session POST /users/sign_in(.:format) sessions#create
destroy_user_session DELETE /users/sign_out(.:format) sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) account#cancel
user_registration POST /users(.:format) account#create
new_user_registration GET /users/sign_up(.:format) account#new
edit_user_registration GET /users/edit(.:format) account#edit
PATCH /users(.:format) account#update
PUT /users(.:format) account#update
DELETE /users(.:format) account#destroy
root GET / welcome#index
destroy_plan DELETE /plans/:id(.:format) plans#destroy
plan_replies GET /plans/:plan_id/replies(.:format) replies#index
POST /plans/:plan_id/replies(.:format) replies#create
edit_plan_reply GET /plans/:plan_id/replies/:id/edit(.:format) replies#edit
plan_reply PATCH /plans/:plan_id/replies/:id(.:format) replies#update
PUT /plans/:plan_id/replies/:id(.:format) replies#update
DELETE /plans/:plan_id/replies/:id(.:format) replies#destroy
plans GET /plans(.:format) plans#index
POST /plans(.:format) plans#create
new_plan GET /plans/new(.:format) plans#new
edit_plan GET /plans/:id/edit(.:format) plans#edit
plan PATCH /plans/:id(.:format) plans#update
PUT /plans/:id(.:format) plans#update
destroy_user DELETE /users/:id(.:format) users#destroy
/*path(.:format) welcome#error_404
Routes for RuCaptcha::Engine:
root GET / ru_captcha/captcha#index
当我输入 url 路径为"http://localhost:3000/plans/85/edit85"这个:plan_id并不存在),发现会出现这样的错误:"时 (事实上"
Couldn't find Plan with 'id'=85
Extracted source (around line #155):
record = s.execute([id], self, connection).first
unless record
raise RecordNotFound, "Couldn't find #{name} with '#{primary_key}'=#{id}"
end
record
rescue RangeError
但是我看到 ruby-china 源码里面似乎也没怎么使用 rescue 之类的来处理这种问题,于是想问问各位,怎么实现遇到这种找不到 plan 能够跳转到某某 404 页面的效果呢?