2011-08-19 55 views
0

我想讀取我的表的所有記錄。這是我的代碼關於路由的問題

控制器:

def list 
    @vorlesung=Vorlesung.find(:all) 
    end 

查看:

<html> 
<body> 
    <table> 
    <tr> 
     <th> 
     Name 
     </th> 
    </tr> 
    <% @vorlesung.each do |v| %> 
    <tr> 
     <td><%= v.Name %> </td> 
    </tr> 
    <% end %> 
    </table> 

</body> 

</html> 

和擊潰文件:

root :to => 'vorlesungs#Show' 
    match 'vorlesungs/new' =>'vorlesungs#new' 
    match 'vorlesungs' =>'vorlesungs#list' 
resources :vorlesungs 

我瞭解的CRUD我schould只寫資源:vorlesungs。但我的代碼不起作用沒有匹配'vorlesungs'=>'vorlesungs#列表'你會有人告訴我這個理由嗎?

回答

1

列表不是由資源創建的默認資源。查看下面鏈接中的表格,查看使用resources方法創建的路由所使用的所有操作。

http://guides.rubyonrails.org/routing.html#resources-on-the-web

您應該保持在你的路線或重命名列表索引(包括你的觀點list.html.erb)。我建議重命名列表來索引以避免打破常規。

+0

謝謝,當我在控制器中更改我的函數的名稱來索引和刪除匹配'vorlesungs'=>'vorlesungs#list'時,這不起作用,並且出現錯誤:NoMethodError,我很抱歉,我是初學者:( –

0

上市資源的所有元素的CRUD操作是指數,所以如果你改變你的代碼

def index 
    @vorlesung=Vorlesung.find(:all) 
end 

,並重新命名您的視圖索引,那麼你將能夠使用

resources :vorlesung