2011-05-23 95 views
1

這是我的問題。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 

然而,當@usernil我收到以下錯誤:

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> 

我在這裏做錯了什麼?

回答

5

試試這一個,它可以幫助你,THX

if [email protected]? 
respond_to do |format| 
    format.html # home.html.erb 
end 
else 
    raise ActiveRecord::RecordNotFound 
end 

我Rails應用,這是我的方式來處理404,我application_controller.rb裏面我這樣的代碼:

unless Rails.application.config.consider_all_requests_local 
rescue_from Exception, :with => :render_error 
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found 
rescue_from ActionController::UnknownController, :with => :render_not_found 
rescue_from ActionController::UnknownAction, :with => :render_not_found 
end 

private 
def render_not_found(exception) 
    render :template =>"/error/404", :status => 404 
end 

def render_error(exception) 
    render :template =>"/error/500", :status => 500 
end 

和配置/環境內/ development.rb確保:

config.consider_all_requests_local = true 
+0

它只是將錯誤更改爲控制器#home中的ActiveRecord :: RecordNotFound ..仍然沒有將我重定向到404:h – Lucas 2011-05-23 10:36:08

+0

hmm,那麼您的config/environments目錄中的development.rb,請確保config .consider_all_requests_local爲真 – 2011-05-23 17:01:44

+0

我以不同的方式做了它(自定義404頁面),但您的解決方案也可以。謝謝:) – Lucas 2011-05-24 06:55:01

4

1)內的一個ApplicationController的DD:

rescue_from ActiveRecord::RecordNotFound do |exception| 
    render_404 
    end 

    def render_404 
    respond_to do |format| 
     format.html { render "errors/404", :status => '404 Not Found', :layout => false } 
     format.xml { render :nothing => true, :status => '404 Not Found' } 
    end 
    true 
    end 

2)查看文件夾裏面創建一個名爲「錯誤

3子文件夾)的電流404.html文件複製到新的「意見/錯誤」的文件夾並將其重命名「404.html.erb

側面說明:

就個人而言,我喜歡創建自定義404的內容,然後使用應用程序的佈局。我更喜歡這樣渲染500個錯誤。

+0

謝謝,我會切換到自定義404錯誤。我很驚訝,我得到錯誤,試圖使用原始的404錯誤。無論如何,再次感謝。 – Lucas 2011-05-24 11:38:55

+0

優秀。良好的小型框架來處理這些。 – Dogweather 2012-10-09 04:09:50