public下文件页面引用app下的js,css以及配置404 500跳转

本文介绍如何在Rails环境中正确配置404错误页面,包括调整开发与编译环境下的资源引用方式,并通过修改配置文件实现自定义的错误页面显示。
自己在做404跳转页面的时候把404放在public文件下,然后按照平常的方式
<%= stylesheet_link_tag "name"%>
<%= javascript_include_tag "name"%>
用上面之后就会出现在开发模式下能正常运行,但是在编译环境下就不能运行,
应该改成静态方式应用
<link href="/assets/tipsy.css" media="screen" rel="stylesheet" type="text/css" />
<script src="/assets/jquery-1.7.2.min.js" type="text/javascript"></script>
至于配置可以参考给力链接,我也复制一份,主要防止其它文档删除,难找

首先在 config/environment/development.rb中,找到下面这句代码,将其设为false
config.consider_all_requests_local = false # rails 4.0
或者
config.action_controller.consider_all_requests_local = false # rails 3.0
接着修改route.rb, 在route.rb中增加下面这句:(注意:放到最后一行)
# make sure this rule is the last one
get '*path' => proc { |env| Rails.env.development? ? (raise ActionController::RoutingError, %{No route matches "#{env["PATH_INFO"]}"}) : ApplicationController.action(:render_not_found).call(env) }

然后在application_controller.rb中增加下面代码:
def self.rescue_errors
rescue_from Exception, :with => :render_error
rescue_from RuntimeError, :with => :render_error
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
rescue_from ActionController::RoutingError, :with => :render_not_found
rescue_from ActionController::UnknownController, :with => :render_not_found
rescue_from ActionController::UnknownAction, :with => :render_not_found
end

rescue_errors unless Rails.env.development?

def render_not_found(exception = nil)
render :file => "/public/404.html", :status => 404
end

def render_error(exception = nil)
render :file => "/public/500.html", :status => 500
end
完成以上配置以及跳转方法

给力链接:
http://www.cnblogs.com/lmei/p/3266170.html
<think>根据用户需求,需要在FastAdmin中配置自定义404错误页面。FastAdmin基于ThinkPHP5框架,因此配置404页面需要修改ThinkPHP的配置和模板。 步骤: 1. 在应用目录(application)下创建error.php文件,用于定义错误页面模板。 2. 修改配置文件,指定错误跳转的模板文件。 具体操作如下: 第一步:创建错误页面模板 在`application`目录下创建`view`目录(如果不存在则创建),然后在`view`目录下创建`error`目录,并在`error`目录下创建`404.html`文件。例如:`application/view/error/404.html`。 在`404.html`中编写自定义的404页面内容,例如: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>页面不存在</title> </head> <body> <h1>404 页面不存在</h1> <p>您访问的页面不存在,请检查网址是否正确</p> </body> </html> ``` 第二步:修改配置文件 打开`application/config.php`文件,找到`http_exception_template`配置项,如果没有则添加。该配置项用于指定各种HTTP异常对应的模板文件。 修改配置如下: ```php // 异常页面的模板文件 'http_exception_template' => [ // 定义404错误的模板文件路径 404 => APP_PATH . 'application/view/error/404.html', // 还可以定义其他的HTTP错误,如500500 => APP_PATH . 'application/view/error/500.html', ], ``` 注意:这里的路径是绝对路径,使用`APP_PATH`常量(指向application目录的上级目录,即项目根目录)来拼接。因为我们的404.html文件放在`application/view/error/`下,所以路径为`APP_PATH . 'application/view/error/404.html'`。 第三步:测试 为了测试配置是否生效,可以访问一个不存在的URL,查看是否跳转到了自定义的404页面。 注意事项: 1. 确保服务器配置正确,特别是Nginx或Apache的配置,确保在遇到404错误时由ThinkPHP框架处理,而不是服务器默认的404页面。 2. 如果开启了URL重写,请确保重写规则正确。 关于服务器配置(以Nginx为例): 在Nginx配置中,需要将请求转发给FastCGI处理,并设置`fastcgi_intercept_errors`为`on`,这样Nginx才会将错误交给后端处理。但通常ThinkPHP框架会处理404错误,所以只要Nginx的配置正确指向了入口文件,一般不需要额外设置。 示例Nginx配置片段: ``` server { listen 80; server_name example.com; root /path/to/fastadmin/public; index index.php index.html; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; } } ``` 如果按照以上步骤配置后,仍然显示服务器默认的404页面,请检查: - 确保`http_exception_template`配置正确,路径无误。 - 确保应用调试模式关闭(`app_debug`设置为`false`),因为在调试模式下,框架会显示详细的错误信息而不是自定义错误页面。在`application/config.php`中设置`'app_debug' => false`。 引用说明: 根据引用[1]中提到的调试模式,在配置错误页面时,需要确保调试模式关闭,否则不会显示自定义错误页面。因此,在正式环境中,请将`app_debug`设置为`false`。 另外,如果自定义错误页面需要复杂的样式和布局,可以按照FastAdmin的模板结构来设计,引入公共的CSSJS文件,保持页面风格统一。 相关问题: 1. 如何在FastAdmin中配置500错误页面? 2. 如何让自定义错误页面使用FastAdmin的后台布局? 3. 在FastAdmin中如何调试错误页面配置不生效的问题?</think>在FastAdmin中配置自定义404错误页面,主要通过修改ThinkPHP框架的错误处理机制实现。以下是具体步骤: ### 配置步骤 1. **创建自定义404模板文件** 在 `/application/view/error/` 目录下创建 `404.html` 文件(若目录不存在请手动创建),内容示例: ```html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>页面不存在</title> <!-- 引入FastAdmin公共样式 --> <link href="__CDN__/assets/css/backend.css" rel="stylesheet"> </head> <body> <div class="error-page text-center"> <h1>404</h1> <p>您访问的页面不存在</p> <a href="/" class="btn btn-primary">返回首页</a> </div> </body> </html> ``` 2. **修改配置文件** 编辑 `/application/config.php`,在配置数组中添加: ```php 'http_exception_template' => [ 404 => APP_PATH . 'view/error/404.html', // 指定404模板路径 // 可扩展其他错误码如500 ], 'app_debug' => false, // 必须关闭调试模式[^1] ``` 3. **配置伪静态规则(Nginx示例)** 在Nginx配置中确保错误由FastAdmin处理: ```nginx server { location / { try_files $uri $uri/ /index.php?$query_string; } error_page 404 /index.php; # 强制404走FastAdmin路由 } ``` 4. **测试效果** 访问不存在的URL(如 `yoursite.com/undefined-page`),将显示自定义404页面。 ### 关键注意事项 1. **调试模式必须关闭** 确保 `app_debug = false`,否则系统会显示默认错误详情页[^1]。 2. **模板路径正确性** 路径 `APP_PATH . 'view/error/404.html'` 中的 `APP_PATH` 指向 `/application/` 目录。 3. **样式统一建议** 引入FastAdmin的CSS文件(如示例中的 `backend.css`)可保持后台风格统一。 4. **服务器配置同步** Apache用户需检查 `.htaccess` 是否包含: ```apache ErrorDocument 404 /index.php ``` ### 常见问题排查 - **自定义页面不生效** ① 检查 `app_debug` 是否关闭 ② 清除 `runtime/temp` 目录缓存 ③ 查看Nginx/Apache错误日志定位问题[^1] - **页面样式错乱** 检查CSS路径是否正确,建议使用 `__CDN__` 宏替换资源路径 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值