当我得到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)
最佳答案
SELECT * FROM books
返回nil
但是SELECT * from book
返回1个对象。
制作您的表名簿。Book.all
从book表而不是book表中获取记录。
关于mysql - ruby on rails mysql没有结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42835813/