2017-03-16 39 views
0

當我拿到書的數據,它沒有返回值Ruby on Rails的MySQL的不返回結果

這裏是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) 
+1

您有沒有書籍表 –

+0

中的任何數據?您可以發佈「Book.count」的結果嗎? –

+0

Book.count返回0.但該表有1行...爲什麼會發生這種情況?(我更新了我的問題) –

回答

0

SELECT * FROM books收益爲零 但SELECT * from book返回1個對象。 製作你的餐桌書。
Book.all從書籍表格而非書籍表格中提取記錄。

+0

謝謝!它的工作:) –

+0

接受答案,如果它幫助你:) – Sajin

+0

如何接受答案...? –