3

嘿,我是 Rails 新手,每次访问 localhost:3000/ 时都会出现此错误

路由错误

没有路线匹配 [GET] "/" 尝试运行 rake 路线以获取有关可用路线的更多信息。

这就是我的 routes.rb 文件的样子。

SampleApp::Application.routes.draw do
  get "static_pages/about"
  get "static_pages/contact"
  get "static_pages/help"
  get "static_pages/home"

end

我的意见文件夹看起来像这样

views/static_pages/about.html.erb
views/static_pages/contact.html.erb
views/static_pages/help.html.erb
views/static_pages/home.html.erb

我运行了 rake routes 命令并得到以下信息:

Davids-MacBook-Air:sample_app DavidStevenson$ rake routes
   static_pages_home GET /static_pages/home(.:format)    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
static_pages_contact GET /static_pages/contact(.:format) static_pages#contact

任何想法出了什么问题?

4

3 回答 3

5

您只是忘记定义您的根路由。将其放在routes文件的末尾:

root to: 'static_pages#home'

当您访问时,您现在将看到 static_pages_home 页面localhost:3000/

查看本指南以获取您需要的有关路线的所有信息。

编辑

这是您的routes文件的外观:

SampleApp::Application.routes.draw do
  get "static_pages/about"
  get "static_pages/contact"
  get "static_pages/help"
  get "static_pages/home"

  root to: 'static_pages#home'
end
于 2013-02-11T20:00:01.490 回答
1

您没有设置根路由,并且"/"正在尝试与之匹配。把它放在最后。

# config/routes.rb
root to: "static_page#home"
于 2013-02-11T20:00:18.690 回答
1

添加根路由

root :to => 'static_pages#home'

把它放在路线块的末尾

于 2013-02-11T20:00:23.823 回答