简体   繁体   English

Rails:重定向到原始404页面时缺少模板错误

[英]Rails: Error missing template when redirecting to original 404 page

Here's my problem. 这是我的问题。

In my controller, I check that the params given in the URL is a valid user, IF NOT I want the visitor to be redirected to the "classic" 404 page. 在我的控制器中, 如果不希望访问者重定向到“经典” 404页面,则检查URL中给定的参数是否是有效用户。 Here's my controller : 这是我的控制器:

def home
  @user = User.find_by_domain(params[:domain])

  if !@user.nil?
    respond_to do |format|
      format.html # home.html.erb
    end
    else
      render(:file => "#{RAILS_ROOT}/public/404.html", :status => 404, :layout => false)
    end
end

Yet, when the @user is nil I get the following error: 但是,当@usernil时, @user以下错误:

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

I didn't modify the 404 page that Rails generates: 我没有修改Rails生成的404页面:

<!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>

What am i doing wrong here? 我在这里做错了什么?

Try this one, may it help you, Thx 试试这个,可能对您有帮助,Thx

if !@user.nil?
 respond_to do |format|
   format.html # home.html.erb
 end
else
  raise ActiveRecord::RecordNotFound
end

I've rails apps and this is my way to handle 404, inside my application_controller.rb i've code like : 我已经安装了rails应用程序,这是在我的application_controller.rb内部处理404的方式,我的代码如下:

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

And inside config/environments/development.rb make sure: 在config / environments / development.rb中,请确保:

config.consider_all_requests_local = true

1) Inside your ApplicationController add: 1)在您的ApplicationController中添加:

  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) Inside the Views folder create a sub folder called " errors " 2)在“视图”文件夹中,创建一个名为“ errors ”的子文件夹

3) Copy your current 404.html file into the new "views/errors" folder and rename it " 404.html.erb " 3)将您当前的404.html文件复制到新的“ views / errors”文件夹中,并将其重命名为“ 404.html.erb

Side Note: 边注:

Personally, I like to create custom 404 content and then use the application's layout. 就个人而言,我喜欢创建自定义404内容,然后使用应用程序的布局。 I prefer to render 500 errors this way too. 我也喜欢这样渲染500个错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM