2010-10-26 44 views
0

我對Rails很新。首先列出所有用戶,開始我的學習過程。已解決:在rails中列出用戶+模板錯誤

,我首先加入一個控制器中的行動

def list_users 
    @users=User.find(:all) 
end 

並在視圖users/list_users.html.erb我添加了這行

list_users.html.erb

<%= Time.now %> 

<% @users.each do |user| %> 
    <%= user.firstname %> 
<% end %> 

而對於路由我已添加路線爲

map.list_users '/list_users', :controller => 'users', :action => 'list_users' 

完蛋了..當我跑我的應用程序,它顯示我的錯誤作爲

 Development mode eh? Here is the error - #<ActionView::TemplateError: 
    ActionView::TemplateError (You have a nil object when you didn't expect it! 
     You might have expected an instance of Array. 
     The error occurred while evaluating nil.each) on line #7 of app/views/users/list_users.html.erb: 

爲什麼會這樣?請給一些建議..

解決方案:

我自己發現控制器正在採取行動保護,這就是爲什麼它顯示我的錯誤。對不起,問愚蠢的問題 謝謝大家的幫助。

+0

你調用一個不存在的方法,因爲錯誤狀態。沒有更多的上下文,就像任何人都能告訴你的一樣。 – 2010-10-26 05:54:54

回答

1

看來你沒有在list_users.html.erb中使用login方法,你在哪裏使用它?

但這裏是另一個錯誤(也許錯字錯誤只?)

<% @users.each do |user| %> 
    <%= user.firstname # not @user here! %> 
<% end %> 
+0

我仍然得到相同的錯誤.. 您可能已經預期了一個Array的實例。 評估nil.each時發生的錯誤)在app/views/users/list_users.html.erb的第7行: - 即使將@user更改爲user.firstname .. – useranon 2010-10-26 06:08:45

+0

因此,它抱怨'@ users'是null ...你能否在你的控制器中提供更多的代碼並查看? – PeterWong 2010-10-26 06:44:22

相關問題