我對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:
爲什麼會這樣?請給一些建議..
解決方案:
我自己發現控制器正在採取行動保護,這就是爲什麼它顯示我的錯誤。對不起,問愚蠢的問題 謝謝大家的幫助。
你調用一個不存在的方法,因爲錯誤狀態。沒有更多的上下文,就像任何人都能告訴你的一樣。 – 2010-10-26 05:54:54