這是我的問題。Rails:重定向到原始404時缺失模板頁面
在我的控制器中,我檢查URL中給出的參數是否爲有效用戶,IF NOT我希望訪問者被重定向到「傳統」404頁面。這裏是我的控制器:
def home
@user = User.find_by_domain(params[:domain])
if [email protected]?
respond_to do |format|
format.html # home.html.erb
end
else
render(:file => "#{RAILS_ROOT}/public/404.html", :status => 404, :layout => false)
end
end
然而,當@user
是nil
我收到以下錯誤:
Template is missing Missing template D:/Project/public/404.html with {:handlers=>[:erb, :rjs, :builder, :rhtml, :rxml], :formats=>[:html], :locale=>[:en, :en]} in view paths
我沒有修改的404頁的Rails生成:
<!DOCTYPE html>
<html>
<head>
<title>The page you were looking for doesn't exist (404)</title>
<style type="text/css">
body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
div.dialog {
width: 25em;
padding: 0 4em;
margin: 4em auto 0 auto;
border: 1px solid #ccc;
border-right-color: #999;
border-bottom-color: #999;
}
h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
</style>
</head>
<body>
<!-- This file lives in public/404.html -->
<div class="dialog">
<h1>The page you were looking for doesn't exist.</h1>
<p>You may have mistyped the address or the page may have moved.</p>
</div>
</body>
</html>
我在這裏做錯了什麼?
它只是將錯誤更改爲控制器#home中的ActiveRecord :: RecordNotFound ..仍然沒有將我重定向到404:h – Lucas 2011-05-23 10:36:08
hmm,那麼您的config/environments目錄中的development.rb,請確保config .consider_all_requests_local爲真 – 2011-05-23 17:01:44
我以不同的方式做了它(自定義404頁面),但您的解決方案也可以。謝謝:) – Lucas 2011-05-24 06:55:01