【问题标题】:Rails Tutorial sample_app fails in Heroku with log: ActionController::RoutingError (No route matches [GET] "/about"):Rails 教程 sample_app 在 Heroku 中失败,日志为:ActionController::RoutingError(没有路由匹配 [GET]“/about”):
【发布时间】:2015-08-10 05:56:59
【问题描述】:

我正在关注 Rails 教程的在线版本。第 3 章中的 Sample_app 在本地可以正常工作,但是当推送到 Heroku 时,会找到主页,但找不到其他页面。在尝试查看 About 页面后运行 heroku 日志会给我(以及其他许多)上面的错误:

2015-08-09T02:56:43.916991+00:00 app[web.1]: ActionController::RoutingError (No route matches [GET] "/about"):

我的 routes.rb 文件是

Rails.application.routes.draw do
  root 'static_pages#home'
  get 'static_pages/help'
  get 'static_pages/about'
end

我已仔细按照指示进行操作。我尝试删除并重新创建 Heroku 文件。我进行了网络搜索并尝试了一些无济于事的事情。我的 gemfile 直接来自本书的在线版本,它使用的是当前版本。


已解决:#thedanotto 让我运行 heroku run rake routes,这表明帮助和关于页面被定向到 {root}/static_pages/about 而不是 {root}/about。我仍然很困惑为什么教程给出了似乎没有按预期工作的关于路线,并欢迎任何进一步的评论,但我将其标记为已解决。

【问题讨论】:

  • 运行命令会得到什么:heroku run rake routes
  • 好建议!我得到 Running rake routes 附加到终端... up, run.3908 Prefix Verb URI Pattern Controller#Action root GET / static_pages#home static_pages_help GET /static_pages/help(.:format) static_pages#help static_pages_about GET /static_pages/about (.:format) static_pages#about 果然,当我运行enigmatic-beach-9653.herokuapp.com/static_pages/about 时,页面出现了!我将在下面更新我的问题。感谢您的帮助!
  • 我将添加一个您可以接受的详细答案。
  • 为了将问题标记为已解决,请不要编辑问题。相反,请mark an answer as accepted。这样一来,您和被接受的用户就可以获得一些声誉积分,并且您可以帮助其他人稍后找到正确的答案。

标签: ruby-on-rails heroku railstutorial.org


【解决方案1】:

每当我找不到路线时,我都会运行终端命令

rake routes

既然你在 heroku 上,你会想要运行

heroku run rake routes

这将返回类似于以下内容的内容。

Prefix Verb        URI Pattern                       Controller#Action
static_pages_about GET /static_pages/about(.:format) static_pages#about

这表明你可以去www.[heroku_app_name].herokuapp.com/static_pages/about 它会带你到你想要的页面。您还可以通过将以下代码行放入视图中来在视图中添加指向页面的链接。

<%= link_to("About", static_pages_about_path) %>

这些都是好东西。但是让我们谈谈使用控制器操作:static_pages#about 和路径/about

routes.rb中的以下内容从

get 'static_pages/about'

get "static_pages/about", path:"/about"

或者

get "/about", to: "rabbits#about"

你可以阅读更多关于Routes here

【讨论】:

    【解决方案2】:

    如果它在本地运行良好,我假设您已经正确设置了控制器、视图等

    您是否确保提交所有这些必要的更改然后推送?

    例如

    git add .
    git commit -am "Added route"
    git push heroku master
    

    【讨论】:

    • 很好的建议,但是是的,我做了这些事情。我已经取得了进步——请看我进一步的 cmets。
    【解决方案3】:

    您是否使用以下 URL 访问“关于”页面? http://YourHerokuAppName.herokuapp.com/static_pages/about

    [将“YourHerokuAppName”替换为您的 Heroku 应用名称]

    【讨论】:

    • 我正在通过enigmatic-beach-9653.herokuapp.com 访问主页,这是可行的消息。
    • @WahhabB.. 确切地说,enigmatic-beach-9653.herokuapp.com/about 正在向您抛出此错误,因为您尚未在 route.rb 文件中为此定义任何路线。如果你想使用 enigmatic-beach-9653.herokuapp.com/about 访问 about 页面,那么你可以简单地在 route.rb 文件中定义一个命名路由。只需替换: get 'about' => 'static_pages#about' 获取 'static_pages/about'
    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-21
    • 2017-03-21
    • 1970-01-01
    相关资源
    最近更新 更多