【问题标题】:Can't get Heroku to serve public index.html page无法让 Heroku 为公共 index.html 页面提供服务
【发布时间】:2018-11-16 01:21:17
【问题描述】:

过去几天我一直在尝试在 Heroku 上启动我的项目 https://github.com/robdrosenberg/news-hunt,但 heroku 不会提供我的 public/index.html 页面。

这是我目前遇到的错误:

ActionController::RoutingError (No route matches [GET] "/")

整个 StackOverflow 都出现了这个错误,我已经尽可能多地尝试了这些解决方案。

例如,直接通过欢迎控制器路由到文件也会给我一个错误

路线

Rails.application.routes.draw do
  post 'user_token' => 'user_token#create'
  post 'users' => 'users#create'
  namespace :api do
    get 'reddit' => 'posts#reddit'
    get 'producthunt' => 'posts#producthunt'
    get 'medium' => 'posts#medium'
    get 'hackernews' => 'posts#hackernews'
    get 'githubtrending' => 'posts#githubtrending'
    get 'all' => 'posts#all'
    get 'bookmarks' => 'bookmarks#index'
    post 'bookmarks' => 'bookmarks#create'
    delete 'bookmarks' => 'bookmarks#destroy'
  end
  root 'welcome#index'
end

控制器

class WelcomeController < ApplicationController
  def index
    render file: Rails.root.join('public','index.html')
  end
end

错误

ActionView::MissingTemplate (Missing template public/index.html with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: app/app/views

将 config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 更改为 = true 并没有什么不同。

我尝试使用重定向,导致应用因重定向过多而崩溃。

我觉得奇怪的是,当我尝试直接渲染文件时,它会在 app/views 文件夹中搜索。在本地一切正常,所以它必须与生产环境和 Heroku 相关。

我在 API 模式下使用 Rails,并在我的 index.html 文件中通过 CDN 使用 Vue。

我以同样的方式部署了一个不同的项目,没有任何问题。您可以在此处找到该代码库:https://github.com/robdrosenberg/commitment-ledger。

非常感谢任何帮助!

【问题讨论】:

  • 默认情况下,Rails 将在应用程序视图目录中查找。具体来说,它将寻找一个名为welcome 的文件夹,因此app/views/welcome 有一个名为index.html.erb 的文件,最终结果是app/views/welcome/index.html.erb,这是Rails 所期望的。如果你看一下路线,root 'welcome#index' 会很有意义,所以welcome 是视图目录中的文件夹,index.html.erb 是它要查找的文件。
  • 你试过/index吗?
  • @RockwellRice 不会根 'welcome#index' 路由到我的欢迎控制器中的 index 方法吗?我有指向它应该呈现的文件。
  • @SimonBrazell 我应该把 /index 放在哪里?不明白你的意思抱歉!

标签: ruby-on-rails heroku deployment


【解决方案1】:

您实际上在该 repo 中没有public/index.html 页面,而是public/Index.html(注意“i”和“I”的大小写)。如果您在 Windows 或 Mac 上开发,这不会有太大区别,但在 Linux(用于 Heroku)上,它们将被视为不同的文件。

将文件重命名为index.html(使用git mv,不要忘记提交),它应该可以工作。

【讨论】:

  • 这就是问题所在。我检查了它,但我没有使用 git mv。我正式感到愚蠢,但我仍在学习。感谢您的帮助!
【解决方案2】:

您是否有理由要重定向到公共/索引?当然,我不知道你的应用程序,所以这可能没有帮助。您是否考虑过将 index.html 文件移动到 views/welcome 目录?这是welcome#index 方法渲染文件的默认位置。

【讨论】:

  • 我不想重定向到那个文件,但我已经根据 Stack Overflow 中其他人的解决方案进行了尝试。我还没有尝试将我的 index.html 文件移动到视图文件夹。是否需要一个 index.html.erb 文件才能在其中工作?
  • 我尝试将索引文件移动到欢迎视图文件夹并路由到它。我收到了这个错误:Missing template Users/robertrosenberg/news-hunt/views/welcome/index.html with {:locale=&gt;[:en], :formats=&gt;[:html], :variants=&gt;[], :handlers=&gt;[:raw, :erb, :html, :builder, :ruby, :jbuilder]}. Searched in: * "/Users/robertrosenberg/news-hunt/app/views" * "/Users/robertrosenberg/news-hunt" * "/"
  • 听起来@matt 可能已经发现了这两个错误的问题,因为文件名在 Linux 中是区分大小写的。
【解决方案3】:

尝试将此添加到您的 config/routes.rb 文件中:

get '', to: redirect('/Index.html')

这样,对应用程序根目录的请求将重定向到公用文件夹中的索引文件。

如下所述,您已将文件命名为大写“I”,在上面的重定向中引用它时,您需要使用相同的大小写,如已更新。

【讨论】:

  • 这是我在尝试您的解决方案时遇到的错误。 ActionController::RoutingError (No route matches [GET] "/public/index.html"): 当我在 heroku bash 中检查时,公共文件中确实存在 index.html 文件。
  • 这很奇怪,老实说,我只尝试使用本地环境中公共文件夹中已经存在的文件,例如404.html 等可能尝试将文件重命名为索引以外的其他内容?
  • 已更新以包含正确的文件名,请使用更新后的重定向重试。
猜你喜欢
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
  • 2017-01-04
  • 2014-06-27
  • 2015-10-06
  • 2013-11-04
  • 2020-05-13
  • 1970-01-01
相关资源
最近更新 更多