2012-03-29 52 views
0

我最近開始學習RoR。我的問題是,當我在html.erb文件中調用帖子時,我收到了意外的輸出。意外佈局輸出 - RoR 3.0.1.1

1 class TestSiteController < ApplicationController 
2 def Home 
3  @greeting = 'Hello' 
4  @posts = Post.all 
5 
6  respond_to do |format| 
7  format.html 
8  format.xml { render :xml => @posts } 
9  format.json { render :json => @posts } 
10  end 
11 end  


1 <h1>testsite#home</h1> 
2 <%= link_to "contact", about_us_path %> 
3 
4 <table> 
5 <%= @posts.each do |p| %> 
6 <tr> 
7 <td><%= p.topic %></td> 
8 <td><%= p.post %> </td> 
9 </tr> 
10 <% end %> 
11 </table> 

我得到我想要的帖子列表,但在列表頂部是我不希望輸出的帖子數組。任何人都知道這是爲什麼發生?

下面是輸出:

//Unwanted array 
[#<Post id: 1, topic: "Rainy Day", post: "Today was a rainy day", created_at: "2012-03-29 21:22:43", updated_at: "2012-03-29 21:22:43">, #<Post id: 2, topic: "It works!", post: "Saving data from the rails console to the mysql db ...", created_at: "2012-03-29 21:25:18", updated_at: "2012-03-29 21:25:18">] 
//List start 
Rainy Day Today was a rainy day 
It works! Saving data from the rails console to the mysql db work! I'm so ecstatic! :D 

回答

1

你,因爲你使用<%=輸出的@posts內容。您應該將其更改爲<%

<% @posts.each do |p| %> 
+0

哇,這完全解決了我的問題!謝謝! – cj3kim 2012-03-29 22:19:55