2

在阅读 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
4

3 回答 3

6

看你没有任何路线"/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 helper

大概有:

about_path或者about_url应该使用

联系方式

contact_path或者contact_url

求助

help_path或者help_url

于 2013-09-27T09:52:42.833 回答
3

你必须使用这些:

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-->       
于 2013-09-27T09:51:45.677 回答
2

你的路径static_pages#about

/关于

代替

/static_pages/关于

于 2013-09-27T09:31:36.607 回答