不需要时显示的模型哈希
Hash of model displaying when not desired
我正在为 Rails 的 Ruby 中的演示文稿注册申请。
因此,我创建了几个模型,包括学生和管理员模型。我在 table 和 bootstrap 中显示来自这些模型的数据,如下所示:
<table class="table">
<thead>
<tr>
<th scope="col">First name</th>
# ...
</tr>
</thead>
<tbody>
<%= @student.each do |stud| %>
<tr>
<td scope="row"><%= stud.Firstname %></td>
# ...
</tr>
<% end %>
</tbody>
我的控制器:
def list
@student = Student.all.order(:Firstname)
end
问题是应用程序将数据库中所有对象的列表打印为哈希。
#<Presentation id: 3, Name: "Elon Musk", Year: "6 Gc", Title: "Electric Cars", Subject: "Phsics", Mentor: "Alberto Maraffio", Room: "N364", From: "13:45", Until: "14:00", Date: "07.11.18", Free: 5, Occupied: 0, Visitors: nil, created_at: "...", updated_at: "...">, #<Presentation id: 3, # ...
这不在 layouts/application.html.erb 文件中,我让它消失的唯一方法是注释掉 <%= yield %>
,这当然会隐藏页面的其余部分也是如此。我做错了什么?
def list
@student.each = Student.all.order(:Firstname)
end
这对我来说很奇怪!
应该是这样的:
def list
@students = Student.order(:Firstname)
end
那么你的HTML也应该是这样的:
<table class="table">
<thead>
<tr>
<th scope="col">First name</th>
# ...
</tr>
</thead>
<tbody>
<% @students.each do |stud| %>
<tr>
<td scope="row"><%= stud.Firstname %></td>
# ...
</tr>
<% end %>
</tbody>
这一行
<%= @student.each do |stud| %>
不应显示在页面中。移动到
<% @student.each do |stud| %>
除此之外,我不明白你想用这一行做什么
@student.each = Student.all.order(:Firstname)
应该是
@student = Student.order(:Firstname)
并注意 @students
可能是这个变量的更好名称
我正在为 Rails 的 Ruby 中的演示文稿注册申请。 因此,我创建了几个模型,包括学生和管理员模型。我在 table 和 bootstrap 中显示来自这些模型的数据,如下所示:
<table class="table">
<thead>
<tr>
<th scope="col">First name</th>
# ...
</tr>
</thead>
<tbody>
<%= @student.each do |stud| %>
<tr>
<td scope="row"><%= stud.Firstname %></td>
# ...
</tr>
<% end %>
</tbody>
我的控制器:
def list
@student = Student.all.order(:Firstname)
end
问题是应用程序将数据库中所有对象的列表打印为哈希。
#<Presentation id: 3, Name: "Elon Musk", Year: "6 Gc", Title: "Electric Cars", Subject: "Phsics", Mentor: "Alberto Maraffio", Room: "N364", From: "13:45", Until: "14:00", Date: "07.11.18", Free: 5, Occupied: 0, Visitors: nil, created_at: "...", updated_at: "...">, #<Presentation id: 3, # ...
这不在 layouts/application.html.erb 文件中,我让它消失的唯一方法是注释掉 <%= yield %>
,这当然会隐藏页面的其余部分也是如此。我做错了什么?
def list
@student.each = Student.all.order(:Firstname)
end
这对我来说很奇怪!
应该是这样的:
def list
@students = Student.order(:Firstname)
end
那么你的HTML也应该是这样的:
<table class="table">
<thead>
<tr>
<th scope="col">First name</th>
# ...
</tr>
</thead>
<tbody>
<% @students.each do |stud| %>
<tr>
<td scope="row"><%= stud.Firstname %></td>
# ...
</tr>
<% end %>
</tbody>
这一行
<%= @student.each do |stud| %>
不应显示在页面中。移动到
<% @student.each do |stud| %>
除此之外,我不明白你想用这一行做什么
@student.each = Student.all.order(:Firstname)
应该是
@student = Student.order(:Firstname)
并注意 @students
可能是这个变量的更好名称