2011-05-23 94 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