Luc*_*cas 1 ruby-on-rails ruby-on-rails-3
这是我的问题.
在我的控制器中,我检查URL中给出的参数是否是有效用户,如果不是我希望将访问者重定向到"经典"404页面.这是我的控制器:
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
Run Code Online (Sandbox Code Playgroud)
然而,当@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
我没有修改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>
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
试试这个,可能对你有帮助,谢谢
if !@user.nil?
respond_to do |format|
format.html # home.html.erb
end
else
raise ActiveRecord::RecordNotFound
end
Run Code Online (Sandbox Code Playgroud)
我是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
Run Code Online (Sandbox Code Playgroud)
在config/environments/development.rb中确保:
config.consider_all_requests_local = true
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5089 次 |
最近记录: |