在我的路線文件我已經指定了:理解「GET」在軌線路
resources :cards do
end
除了基本的CRUD路線我已經另一條路線是如下:
get '/cards/get_schema' => 'cards#get_schema'
當我打這個終點,我實際上被帶到了cards#show
。爲什麼會發生?
在我的路線文件我已經指定了:理解「GET」在軌線路
resources :cards do
end
除了基本的CRUD路線我已經另一條路線是如下:
get '/cards/get_schema' => 'cards#get_schema'
當我打這個終點,我實際上被帶到了cards#show
。爲什麼會發生?
resources :cards
生成的一條路由爲get '/cards/:id'
。你能看到這個問題嗎? get_schema
被識別爲ID。試試這個
resources :cards do
get 'get_schema', on: :collection
end
或者只是放到了首位
get '/cards/get_schema' => 'cards#get_schema'
resources :cards
這取決於定義路由的順序上這條路線。
訂單1
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
resources :cards do
end
get '/cards/get_schema' => 'cards#get_schema'
end
運行路線
rake routes
輸出
~/D/p/p/s/console_test> rake routes
Prefix Verb URI Pattern Controller#Action
cards GET /cards(.:format) cards#index
POST /cards(.:format) cards#create
new_card GET /cards/new(.:format) cards#new
edit_card GET /cards/:id/edit(.:format) cards#edit
card GET /cards/:id(.:format) cards#show #<========
PATCH /cards/:id(.:format) cards#update
PUT /cards/:id(.:format) cards#update
DELETE /cards/:id(.:format) cards#destroy
cards_get_schema GET /cards/get_schema(.:format) cards#get_schema #<========
由於節目預計cards/:id
而且上面/cards/get_schema
它被路由到cards#show
訂購2
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
get '/cards/get_schema' => 'cards#get_schema'
resources :cards do
end
end
的跑動路線
rake routes
輸出
~/D/p/p/s/console_test> rake routes
Prefix Verb URI Pattern Controller#Action
cards_get_schema GET /cards/get_schema(.:format) cards#get_schema #<========
cards GET /cards(.:format) cards#index
POST /cards(.:format) cards#create
new_card GET /cards/new(.:format) cards#new
edit_card GET /cards/:id/edit(.:format) cards#edit
card GET /cards/:id(.:format) cards#show #<========
PATCH /cards/:id(.:format) cards#update
PUT /cards/:id(.:format) cards#update
DELETE /cards/:id(.:format) cards#destroy
在這種情況下/cards/get_schema
將是頂層,不會與cards#show
Rails的衝突處理get_schema
作爲卡的ID。解決的辦法是重新安排航線的聲明,就像這樣:
get '/cards/get_schema' => 'cards#get_schema'
resources :cards do
end
這樣的get_schema
路線將show
路線之前進行匹配。