2012-07-29 116 views
3

当我运行服务器浏览器告诉我这样的事情:路由错误的路由匹配[GET] “/ static_pages /家”,教程

Routing Error 

No route matches [GET] "/static_pages/home" 

Try running rake routes for more information on available routes. 

耙路线显示我:

root/    static_pages#home 
help /help(.:format) static_pages#help 
about /about(.:format) static_pages#about 
contact /contact(.:format) static_pages#contact 

我的routes.rb文件:

MyApp::Application.routes.draw do 
root :to => 'static_pages#home' 

match '/help', :to => 'static_pages#help' 
match '/about', :to => 'static_pages#about' 
match '/contact', :to =>'static_pages#contact' 


end 

有人有想法吗?

+0

没有为/ static_pages /家里没有途径(尽管你家的行动是在/)。你在期待什么? – 2012-07-29 15:28:47

回答

8

没有对URL“/ static_pages /家”

虽然根点static_pages控制器与行动家,它仍然响应路径“/”而不是“/ static_pages /家”

设定路线

如果添加

match '/static_pages/home', :to =>'static_pages#home' 

你会得到 '/ static_pages /家' 的预期反应

+1

谢谢!新手们有些人犯了愚蠢的错误! – szatan 2012-07-29 16:42:25

0

你可以指向"/"root_path而不是'/static_pages/home'。无需两条路线指向同一个地方......

0

你应该增加:如keword定义XXX_path,如 根:到=>“static_pages#家”

match '/', to: 'static_pages#home', :as => :home 
match '/help', to: 'static_pages#help', :as => :help 
    match '/about', to: 'static_pages#about', :as => :about 
    match '/contact', to: 'static_pages#contact', :as => :contact 
2

我刚刚得到跟随Ruby on Rails教程时,与szatan相同的错误。 错误是因为之前我们测试的网址是http://localhost:3000/static_pages/help,因为该操作位于static_pages中。但是,后更改routes.rb

get 'static_pages/help' to 
match '/help', to: 'static_pages#help 

应改为http://localhost:3000/help 的URL,因为我们告诉Rails服务器路由路径/helpstatic_pages#help。我们不应该期望用户知道路径/static_pages/help

1

记得删除“public/index.html”文件。

你可以正确定义所有的路由,但是如果你离开这个文件,你将不能正确地路由它。

0

我在教程中遇到这个问题时遇到了这个问题。当我将浏览器指向"http://localhost:3000/"并重新加载时,它已经解决。

4

步5.3.3 Rails tutorial

您不必刷新页面

http://localhost:3000/static_pages/home 

但只能通过

http://localhost:3000/ 

因为你定义 'static_pages /家' 更改URL作为根'/'。

对我来说,它的工作原理

相关问题