【问题标题】:Rails application not displaying index.html in development modeRails 应用程序在开发模式下不显示 index.html
【发布时间】:2013-09-08 13:54:04
【问题描述】:

我在 Rails 4 应用程序的public 目录中放置了一个index.html 文件,但它仍将这个已删除的文件显示为rails 欢迎页面。我在开发模式下使用 Webrick。我有另一个应用程序在设置中几乎相同,它运行良好。

【问题讨论】:

  • 确保您的文件名或路径中没有错字或多余的空格。这很可能是问题
  • 我已经仔细检查过 - 似乎不是问题。
  • 你重启服务器了吗?
  • 是的,这不是问题。

标签: ruby-on-rails ruby-on-rails-4 webrick


【解决方案1】:

如上所述,Rails 4 已删除 /public 文件夹中的默认 index.html 页面。如果您无论如何都想实现该功能,则必须手动渲染页面。只需创建一个控制器,然后在您选择的控制器操作中呈现索引页面,如下所示:

def index
  render :file => 'public/index.html' and return
end

编辑: 如果您想在不使用 Rails 堆栈的情况下提供文件,则必须配置您的 Web 服务器(Apache/Nginx/whatever)来执行此操作。 NginxApache 的配置。 此外,您必须通过将 serve_static_assets 设置为 false 来在配置中禁用 Rails 的静态文件渲染:

config.serve_static_assets configures Rails itself to serve static assets. Defaults
to true, but in the production environment is turned off as the server software (e.g.
Nginx or Apache) used to run the application should serve static assets instead.
Unlike the default setting set this to true when running (absolutely not recommended!)
or testing your app in production mode using WEBrick. Otherwise you won´t be able use
page caching and requests for files that exist regularly under the public directory
will anyway hit your Rails app.

因此,如果您想在开发模式下使用它,请将environments/development.rb 中的config.serve_static_assets 设置为false。

【讨论】:

  • 那么,如果有一个与域相关联的 Rails 应用程序,您是说如果没有从 Rails 应用程序呈现它,就不可能有一个域指向索引页面?如果我在浏览器中键入 mydomain.com/index.html,我会收到一条没有路由匹配的错误消息。大概这对于每个静态页面都是一样的?我在这里错过了什么吗?
  • 看来我没听懂你的问题。答案现已更新。
【解决方案2】:

【讨论】:

  • 这并不能解决我的问题,因为我想显示我的 index.html 页面。
【解决方案3】:

在导航器中按F5,索引可以在缓存中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-11
    • 1970-01-01
    • 1970-01-01
    • 2023-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 2013-02-28
    相关资源
    最近更新 更多