我學習Ruby on Rails和我這樣做腳手架命令新資源:導軌 - 腳手架工作,錯誤鏈接到作出
rails g scaffold Alex
,它爲我跑和創造的一切資源。所以,我試圖讓從我的索引頁的亞歷克斯頁面像這樣的鏈接:
<%= link_to "Alex Link", alex_path(@alex) %>
(我仍然不相信@alex部分是什麼,但它是在其他的例子,所以我想有有)
,在我的routes.rb這個代碼創建:
resources :alexes
get "home/index"
,當我試圖加載的鏈接,它給了我這個錯誤:
No route matches {:action=>"show", :controller=>"alexes"}
以防萬一,這裏是從rake routes
輸出:
alexes GET /alexes(.:format) alexes#index
POST /alexes(.:format) alexes#create
new_alex GET /alexes/new(.:format) alexes#new
edit_alex GET /alexes/:id/edit(.:format) alexes#edit
alex GET /alexes/:id(.:format) alexes#show
PUT /alexes/:id(.:format) alexes#update
DELETE /alexes/:id(.:format) alexes#destroy
home_index GET /home/index(.:format) home#index
root / home#index
test POST /test(.:format) tests#create
new_test GET /test/new(.:format) tests#new
edit_test GET /test/edit(.:format) tests#edit
GET /test(.:format) tests#show
PUT /test(.:format) tests#update
DELETE /test(.:format) tests#destroy
什麼是錯的,我所做的鏈接,我怎麼可以把它打到控制器它去查看之前的方式嗎?
謝謝!
謝謝!這工作。所以alexes_path會得到所有匹配的實例,而alex_path需要一些id來獲得特定的實例?此外,鏈接呈現後,我點擊它,我在下一頁得到這個錯誤:無法找到表'亞歷克斯'...這是因爲我沒有這樣的表。如果我不想要數據庫調用,我應該沒有指定什麼嗎? – GeekedOut 2012-04-09 19:25:51
如果您不想使用數據庫表來存儲'Alex'對象,則不應該使用腳手架。腳手架建立一個控制器,視圖和模型來支持整個RESTful資源。您需要打開'alexes_controller.rb'並刪除大部分代碼。或者,您需要運行遷移來創建有問題的表。 – meagar 2012-04-09 19:27:29