出于某种原因,这些URL都在不应该的时候路由到同一个文件,在输入无效的URL时我注意到了另一件事,例如localhost:3000 / topics / inexjojvnsjg它只是保持不变页。
这是我尝试访问网址时我的rails控制台告诉我的内容
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
答案 0 :(得分:3)
以下是关键信息:
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
网址是" / topics"。
TopicsController的:show
网址是" / topics /:id"或" / topics / 1",其中网址的最后一部分与params[:id]
相关联。使用网址" / topics / 1" :id
= 1。
所以当你去网址" / topics / index"由于"索引"您将进入:show
行动网址的一部分。您只需将:id
设置为"索引"而不是整数:id
。您可以在此处粘贴的输出中看到:
Parameters: {"id"=>"index"}
TLDR:" / topics / index"是一个将通过Rails路由器的路由但是路由无效,因为:id
是一个字符串"索引"。