Re: [RoR ] 錯誤連結

看板Ruby作者 (怎麼有辦法這麼大聲)時間17年前 (2007/12/12 11:09), 編輯推噓2(201)
留言3則, 2人參與, 最新討論串3/4 (看更多)
※ 引述《ihower (好2ˇ)》之銘言: : ※ 引述《janyfor (妳哪位ㄚ)》之銘言: : : 如果使用者打入了不正確的URL : : 該怎麼告知無此頁面 : : 在每個地方加判斷太麻煩了QQ : : 例如: http://localhost:3000/new : : ↑沒有 new 這個 controller : : http://localhost:3000/news/no : : ↑沒有 no 這個 action : : http://localhost:3000/news/show/66 : : ↑沒有 66 這個 id : : 該怎麼辦呢? : 你這裡有兩種情況: : 1. 沒有這個 route (i.e. 沒有這個controller或action) : 請在 routes.rb 最下面加入如 : map.connect '*anything', :controller => 'lobby', :action => 'page_not_found' : 這樣就會自動導去你自訂的controler跟action : 2. 找不到id, 這時其實Rails是丟 RecordNotFound exception : 請裝 exception_notification 這個官方 plugin, : 他會幫你補捉所有 exception (only in production mode) : 如想自訂 render 頁面, 在 controller/application.rb 裡寫過 : render_404 跟 render_500 就可以了 : 例如我這樣寫 : def render_404 : respond_to do |type| : type.html { render :template => 'common/404', :status => "404 Not Found" } : type.all { render :nothing => true, :status => "404 Not Found" } : end : end 也可以用 rescue_action_in_public 看看 像是 def rescue_action_in_public(exception) case exception when ActiveRecord::RecordNotFound then render :file => "#{RAILS_ROOT}/public/500.html" when NoMethodError render :file => "#{RAILS_ROOT}/public/500.html" else render :file => "#{RAILS_ROOT}/public/500.html" end end 這個如何? 在2.0.1可以寫成 rescue_from(ActiveRecord::RecordNotFound) \\ { |e| render :file => "#{RAILS_ROOT}/public/500.html" } 只是看消息好像ActionController::RoutingError在2是丟404所以無法處理, http://dev.rubyonrails.org/ticket/10328 koji -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.138.150.67 ※ 編輯: kojilin 來自: 140.138.150.67 (12/12 11:10)

12/14 19:52, , 1F
多謝
12/14 19:52, 1F

12/15 14:56, , 2F
不會,忘記寫了,如果在development環境的話還要設定勒
12/15 14:56, 2F

12/17 02:55, , 3F
不好意思 請問可以說一下要設定哪些嗎?
12/17 02:55, 3F
文章代碼(AID): #17Nr3qHL (Ruby)
討論串 (同標題文章)
本文引述了以下文章的的內容:
2
2
以下文章回應了本文
完整討論串 (本文為第 3 之 4 篇):
2
3
2
2
3
5
文章代碼(AID): #17Nr3qHL (Ruby)