2015-05-29 96 views
2

我正在关注Michael Hartl的教程Ruby on Rails教程,并尝试完成3.4.4节的最后一步。当我改变的routes.rb到为什么路由根不起作用?

Rails.application.routes.draw do 
root 'static_pages#home' 
get 'static_pages/help' 
get 'static_pages/about' 
end 

并重新启动http://localhost:3000/static_pages,他们说:“很抱歉,但出事了”。然后我重新启动http://localhost:3000/static_pages/help,它的工作原理。然后,我通过

rake routes 

检查路线,结果表明:

Prefix Verb URI Pattern     Controller#Action 
      root GET/       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_controller.rb内容并没有什么区别的,在教程。有人能告诉我什么是错的吗?

+1

你不是倒退?如果你访问'http:// localhost:3000 /',你的路由定义是不是意味着它会在控制器'static_pages'上调用操作'home'?你想要映射哪个URL(以及哪个控制器/操作)? –

+0

解决了该问题。原谅我,我是网络开发新手。 – Peterxwl

回答

2

好吧,用这个路由映射root 'static_pages#home',你说,当你点击http://localhost:3000/ url时,你会被映射到StaticPages控制器的home动作。

但是你没有定义任何http://localhost:3000/static_pages的映射,它是不正确的url根据route.rb文件。这就是你得到错误的原因。

阅读rake routes输出的第一行,它清楚地告诉你已经定义了什么。

0

该行root 'static_pages#home'相当于get '/', to: 'static_pages#home'。这意味着URL localhost:3000/正在路由到StaticPages控制器上的#home操作。

如果你想localhost:3000/static_pages指向#home行动,加入这一行:

get 'static_pages', to: 'static_pages#home'