当我获得Book的数据时,它不会返回值
这里是list.html.erb文件
<table border="1">
<tr>
<th>ISBN</th>
<th>Name</th>
<th>price</th>
<th>publish</th>
<th>published</th>
</tr>
<%= "asdf" %>
<% @books.each do |book| %>
<%= "asdf" %>
<tr>
<td><%= book.isbn %></td>
<td><%= book.title%></td>
<td><%= book.price%></td>
<td><%= book.publish%></td>
<td><%= book.published%></td>
</tr>
<% end %>
</table>
,这是fm_controller.rb
...
def list
@books = Book.all
@booksCount = Book.count
puts @books
puts @booksCount
end
end
如果您需要更多信息,我很快就会更新。帮助我...
(编辑,更多信息) 服务器日志打印像这样
Started GET "/fm/list" for 127.0.0.1 at 2017-03-16 06:59:48 -0700
Processing by FmController#list as HTML
(0.2ms) SELECT COUNT(*) FROM `books`
Book Load (0.1ms) SELECT `books`.* FROM `books`
0
Rendering fm/list.html.erb within layouts/application
Rendered fm/list.html.erb within layouts/application (0.6ms)
Completed 200 OK in 15ms (Views: 13.0ms | ActiveRecord: 0.2ms)
@bookCount打印0但mysql表有1行。这是我的查询结果
select * from book;
+------+-------+-------+---------+-----------+
| isbn | title | price | publish | published |
+------+-------+-------+---------+-----------+
| 1111 | asdf | 1233 | sasdf | asdf |
+------+-------+-------+---------+-----------+
1 row in set (0.00 sec)
答案 0 :(得分:0)
SELECT * FROM books
返回nil
但SELECT * from book
返回1个对象。
制作你的桌子名册。
Book.all
从books表中获取记录而不是book table。