【问题标题】:Routing Error :: No route matches [GET] "/static_pages/about"路由错误 :: 没有路由匹配 [GET] "/static_pages/about"
【发布时间】:2013-09-27 09:26:19
【问题描述】:

我在阅读 Michael Hartl 的教程时遇到此错误。 第 5.3.2 节(铁路路线)。

我的 routes.rb 文件的内容是

SampleApp::Application.routes.draw do
   root  'static_pages#home'
  match '/help',    to: 'static_pages#help',    via: 'get'
  match '/about',   to: 'static_pages#about',   via: 'get'
  match '/contact', to: 'static_pages#contact', via: 'get'
end

我在 static_pages 的 rspec 是成功的 ..我已按照所有说明进行操作,但 尝试访问 `

处的任何页面时仍然出现此错误
http://localhost:3000/static_pages/home
http://localhost:3000/static_pages/about
http://localhost:3000/static_pages/contact

这是我在这些页面上遇到的错误

动作控制器:捕捉到异常

Routing Error
No route matches [GET] "/static_pages/about"

Rails.root: /home/abhinay/rails_projects/sample_app

Application Trace | Framework Trace | Full Trace
Routes

Routes match in priority from top to bottom



  Helper     HTTP Verb  Path             Controller#Action
    Path / Url          
    root_path     GET       /                    static_pages#home
    help_path     GET      /help(.:format)   static_pages#help
    about_path    GET      /about(.:format)  static_pages#about
    contact_path  GET      /contact(.:format)    static_pages#contact

【问题讨论】:

    标签: ruby-on-rails railstutorial.org custom-routes


    【解决方案1】:

    看你没有任何路线"/static_pages/about"

    所以你有错误

     Routing Error
     No route matches [GET] "/static_pages/about"
    

    根据您关于页面/about 的路线应该是网址

     http://localhost:300/about   
     http://localhost:300/contact 
     http://localhost:300/help
    

    它会真正调用你的 static_pages 控制器和 about 该控制器的操作

    并从您获得的 rails 路线的 url 助手

    关于:

    应该使用about_pathabout_url

    联系方式

    contact_pathcontact_url

    寻求帮助

    help_pathhelp_url

    【讨论】:

    • 哦!我是多么愚蠢..答案总是在我面前,我只是在这里和那里看..实际上我总是在 URl 栏中按“l”之类的 url 的第一个字母,因为它会自动给出所有建议并通过做我从不关心 URL.. 非常感谢.. 问题解决了!!!!
    【解决方案2】:

    你必须使用这些:

    http://localhost:3000/         <!-- Maps to /home -->
    http://localhost:3000/help       <!-- Maps to /static_pages/help-->       
    http://localhost:3000/about       <!-- Maps to /static_pages/about-->       
    http://localhost:3000/contact       <!-- Maps to /static_pages/contact-->       
    

    【讨论】:

    • 感谢好友的回答,我真的很感谢您回答了我的问题..但我没有太多的声誉来投票给答案..可能下次因为我是新手组。
    【解决方案3】:

    您到static_pages#about 的路径是

    /关于

    而不是

    /static_pages/关于

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-26
      • 2016-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-24
      • 1970-01-01
      • 2013-07-19
      相关资源
      最近更新 更多