2012-07-29 61 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作爲根'/'。

對我來說,它的工作原理

相關問題