我首先列出所有用户,开始我的学习过程。
首先,我在控制器中添加了一个动作
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 :(得分:1)
您似乎没有在login
中使用list_users.html.erb
方法,您在哪里使用过它?
但这是另一个错误(可能只是拼写错误?)
<% @users.each do |user| %>
<%= user.firstname # not @user here! %>
<% end %>