例如。我有学生和老师的模特。如果我访问了db中不存在的学生或教师资料,它将提升为ActiveRecord :: RecordNotFound。
我在应用程序控制器中尝试了这个但是仍然收到错误消息 ActiveRecord :: RecordNotFound 而不是 404页。
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :render_404
end
那么,有解决方案吗?
感谢。
答案 0 :(得分:4)
试试这个:)
class ApplicationController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
private
def record_not_found
render file: "#{Rails.root}/public/404", layout: true, status: :not_found
end
end
答案 1 :(得分:0)
在.htaccess文件中写下以下代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^apt_adm - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ error.php [L]