看板 Ruby 關於我們 聯絡資訊
※ 引述《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 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.216.232.59 ※ 編輯: ihower 來自: 61.216.232.59 (12/06 06:45)
janyfor:多謝喔 我試試看 12/06 14:47
godfat:除了板友,好久沒 m 了 @@ 12/06 22:52