我看在RailsCasts一個小插曲,並期待在源代碼集145在Rails中,map.resources現在已經過時了嗎?
這是他對routes.rb
文件
ActionController::Routing::Routes.draw do |map|
map.resources :orders
map.current_cart 'cart', :controller => 'carts', :action => 'show', :id => 'current'
map.resources :line_items
map.resources :carts
map.resources :products
map.resources :categories
map.root :products
end
我立即揭去代碼。這看起來像一個完全不同的語法。然後我意識到這個源代碼是在2010年發佈的。我想知道它是否已經過時,因爲我複製並將該代碼粘貼到我的Rails應用程序中,並且它不起作用。
通常情況下,我做的是
resources 'orders'
root 'products'
我不知道我怎麼會改寫map.current_cart
。
這是錯誤消息我得到
NameError at/orders/new
undefined local variable or method 'map' for #<ActionDispatch::Routing::Mapper::0x4d90868>
此行被突出顯示
map.current_cart 'cart', :controller => 'carts', :action => 'show', :id => 'current'
該代碼是古老的,你不應該試圖從中吸取教訓。這是Rails現在已經被棄用的版本,並且在很多方面都是死路一條。 – meagar 2014-12-05 18:50:06