2015-02-07 38 views
2

由於某些原因,這些URLS路由到相同的文件,當他們不應該,另一件事,當輸入一個無效的URL如localhost:3000/topics/inexjojvnsjg時,它只會停留在同一頁面上。本地主機:3000 /主題/索引和本地主機:3000 /主題/顯示兩個路由到相同的show.html文件

這裏是我的rails控制檯告訴我,當我嘗試訪問的URL localhost:3000/topics/index

Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700 
Processing by TopicsController#show as HTML 
Parameters: {"id"=>"index"} 
Rendered topics/show.html.erb within layouts/application (0.1ms) 
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" =$1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 
Completed 200 OK in 98ms (Views: 96.5ms | ActiveRecord: 0.8ms) 

這裏是我的路線文件....

Rails.application.routes.draw do 

devise_for :users 
get 'welcome/index' 
get 'welcome/about' 

# get "topics/index" 
# get "topics/show" 
# get "topics/new" 
# get "topics/edit" 
#for some reason, using resources:topics, index and show both route to show 
resources :topics 

root to: 'welcome#index' 

post :incoming, to: 'incoming#create' 
end 

回答

3

這裏的關鍵信息:

Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700 
Processing by TopicsController#show as HTML 
Parameters: {"id"=>"index"} 

:index網址爲主題控制器是「/題目」。

TopicsController的:show網址是「/ topics /:id」或「/ topics/1」,其中URL的最後一部分與params[:id]相關聯。與URL「/主題/ 1」:id = 1.

因此,當你去的網址「/主題/索引」你要去:show行動,因爲「索引」部分的網址。您只是將:id設置爲「索引」而不是整數:id。你可以看到,在輸出你粘貼在這裏:

Parameters: {"id"=>"index"} 

TLDR:「/主題/指數」是將通過Rails的路由器,但爲無效路由的路由,因爲:id是一個字符串「指數」 。

+0

該死的!真棒。謝謝。 – 2015-02-07 01:21:33

+0

@BartDangus,如果您認爲正確,請不要忘記接受答案! – 2015-02-07 19:11:18

相關問題