localhost:3000/topics/index 和 localhost:3000/topics/show 都路由到同一个 show.html 文件

localhost:3000/topics/index and localhost:3000/topics/show BOTH route to same show.html file

出于某种原因,这两个 URL 在不应该路由到同一个文件时,我在输入无效 url 时注意到的另一件事,例如 localhost:3000/topics/inexjojvnsjg 它只是停留在同一页上。

这是我的 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" =   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

关键信息如下:

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

TopicsController 的 :index url 是“/topics”。

TopicsController 的 :show url 是“/topics/:id”或“/topics/1”,url 的最后一部分关联到params[:id]。用 url "/topics/1" :id = 1.

因此,当您转到 url“/topics/index”时,您将转到 :show 操作,因为 url 的 "index" 部分.您只是将 :id 设置为 "index" 而不是整数 :id。您可以在此处粘贴的输出中看到:

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

TLDR:“/topics/index”是一条将通过 Rails 路由器的路由,但它是一条无效路由,因为 :id 是一个字符串 "index"。