【问题标题】:Unknown Error with routes.rbroutes.rb 出现未知错误
【发布时间】:2013-02-11 19:55:16
【问题描述】:

嘿,我是 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

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

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

    root to: 'static_pages#home'
    

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

    检查this guide 以获取您需要的有关路线的所有信息。

    编辑

    这是您的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
    

    【讨论】:

    • 那么 routes.rb 文件的格式应该是什么样的呢?像这样?获取“static_pages/about”根目录:'static_pages#about'
    【解决方案2】:

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

    # config/routes.rb
    root to: "static_page#home"
    

    【讨论】:

      【解决方案3】:

      添加根路由

      root :to => 'static_pages#home'
      

      把它放在路由块的末尾

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-05-19
        • 1970-01-01
        • 2014-04-02
        • 1970-01-01
        • 1970-01-01
        • 2021-12-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多