我正在尝试从我的数据库中创建项目列表。问题是我无法在我的视图/用户/index.html.erb 中访问来自 users_controller 方法的变量(数组)。我一直在阅读 tuts 和书籍,但他们告诉我要在路上的某个地方迷恋的方式。任何提示/帮助表示赞赏,谢谢。
控制器:
class UsersController < ApplicationController
def index
render('list')
end
def list
@users = User.all
end
def show
@user = User.find(params[:id])
end
def new
@user = user.new
end
def create
@user = User.new(params[:user])
if @user.save
flash[:notice] = "User succesfully created!"
redirect_to(:action => 'list')
else
flash[:notice] = "User couldn't be created!"
render('new')
end
end
end
list.html.erb
<table>
<tr>
<td>
<th>Name
</td>
<td>
<th>Password
</td>
<td>
<th>Log status
</td>
<td>
<th>Warning
</td>
<td>
<th>Banned
</td>
</tr>
<% @users.each do |u| %>
<tr>
<td><%= u.name %></td>
<td><%= u.password %></td>
<td><%= u.log_status %></td>
<td><%= u.warning %></td>
<td><%= u.banned %></td>
</tr>
<%end%>
</table>
错误:
NoMethodError in Users#index
Showing /home/bogdan/ex/lynda/hW/app/views/users/list.html.erb where line #20 raised:
undefined method `each' for nil:NilClass
Extracted source (around line #20):
17: </td>
18: </tr>
19:
20: <% @users.each do |u|%>
21:
22: <tr>
23: <td><%= u.name %></td>