2014-12-05 61 views
0

我看在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' 
+0

該代碼是古老的,你不應該試圖從中吸取教訓。這是Rails現在已經被棄用的版本,並且在很多方面都是死路一條。 – meagar 2014-12-05 18:50:06

回答

0

在Rails 4+它會是這個樣子:

My::Application.routes.draw do 
    get '/cart', to: 'carts#show', defaults: { id: 'current' } 
end 

這可能是如果這是如何行使路線的話。

map的說法現在消失了。 My::Application取決於有問題的應用程序的名稱。

像往常一樣,有the routing documentation handy當做這樣的事情。

如果您正在使用舊的應用程序,則必須遵循舊的約定。

1

是的,它現在已經過時了。

而不是map.resources只是使用資源。

所以,你應該寫 資源:訂單

相關問題