【问题标题】:started the rails server but localhost page wont load启动了 Rails 服务器,但 localhost 页面无法加载
【发布时间】:2016-11-03 23:39:32
【问题描述】:

我启动了rails服务器

$ bundle exec rails s -p 3001
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://localhost:3001
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2016-11-03 19:29:30] INFO  WEBrick 1.3.1
[2016-11-03 19:29:30] INFO  ruby 2.2.5 (2016-04-26) [x86_64-darwin15]
[2016-11-03 19:29:30] INFO  WEBrick::HTTPServer#start: pid=30949 port=3001

但是当我转到http://localhost:3001 时出现错误:找不到此本地主机页面

这是我继续http://locahhost:3001 后在终端看到的数据 知道有什么问题吗?

{
  "timestamp": "2016-11-03T23:35:40Z",
  "severity": "DEBUG",
  "type": "none",
  "message": "  \u001b[1m\u001b[36mSQL (57.9ms)\u001b[0m  \u001b[1mUSE [peak]\u001b[0m"
}
{
  "timestamp": "2016-11-03T23:35:40Z",
  "severity": "INFO",
  "type": "database.query",
  "request_id": "67b064b7-f6a3-414e-9a39-da4f851fc8dd",
  "connection_id": "70230967245540",
  "event": "SQL",
  "query_id": "a15f740d4daad1a81dbc7e0df1ccf3e5",
  "query": "USE [peak]",
  "duration": "58.3"
}
{
  "timestamp": "2016-11-03T23:35:40Z",
  "severity": "INFO",
  "type": "none",
  "message": "Processing by HomeController#index as HTML"
}
{
  "timestamp": "2016-11-03T23:35:40Z",
  "severity": "INFO",
  "type": "none",
  "message": "  Rendered text template (0.0ms)"
}
{
  "timestamp": "2016-11-03T23:35:40Z",
  "severity": "INFO",
  "type": "view.render",
  "request_id": "67b064b7-f6a3-414e-9a39-da4f851fc8dd",
  "template": "text template",
  "layout": null,
  "duration": "0.4"
}
{
  "timestamp": "2016-11-03T23:35:40Z",
  "severity": "INFO",
  "type": "none",
  "message": "Completed 404 Not Found in 9ms (Views: 8.1ms | ActiveRecord: 0.0ms)"
}
{
  "timestamp": "2016-11-03T23:35:40Z",
  "severity": "INFO",
  "type": "application.request",
  "request_id": "67b064b7-f6a3-414e-9a39-da4f851fc8dd",
  "method": "GET",
  "status": 404,
  "user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36",
  "referrer": null,
  "forwarded_for": null,
  "url_scheme": "http",
  "url_host": "localhost",
  "url_port": "3001",
  "url_path": "/",
  "url_query": null,
  "rails_controller": "home",
  "rails_action": "index",
  "clean_path": "/",
  "remote_ip": "::1",
  "remote_host": "::1",
  "duration": "1108.02",
  "message": "Not Found",
  "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  "content_type": "text/plain; charset=utf-8"
}

【问题讨论】:

  • 您的日志包括Completed 404 Not Found in 9ms,看起来您的应用运行良好,但您提供的路径没有路径。你期待看到什么?
  • 你的路线有什么?最后一条消息说“home#index”是“未找到”,所以路线是开始寻找的好地方......然后......你有一个带有索引操作的家庭控制器(是下一个寻找的地方) .

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2


【解决方案1】:
Completed 404 Not Found in 9ms (Views: 8.1ms | ActiveRecord: 0.0ms)

您的服务器和您的应用程序运行良好;您的浏览器正在加载http://localhost:3001 的页面,非常好。

问题在于您的应用返回 404 Not Found - 换句话说,Rails 无法将控制器/操作与根路由匹配。

查看您的config/routes.rb 文件。您应该为root 定义一个路由,例如

root :to => 'home#index'

如果您直接访问路径(例如http://localhost:3001/example/path),请确保您已为此定义了路由:

get 'example/path', :to => 'controller#action'

最后,确保相关控制器存在并且为您的路由映射到的操作定义了方法(即对于root 示例,确保app/controllers/home_controller.rb 存在并且它定义了index 方法)。

【讨论】:

  • 谢谢。我检查了 routs.rb,他们添加了一个命名空间,所以只需要在 url 中使用命名空间
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-14
  • 2016-05-06
  • 2011-06-19
相关资源
最近更新 更多