2015-12-21 99 views
-1

在我的索引行动我有布局不正确呈现如预期

@posts = Post.all 

这是我的index.html.erb

<div class="row"> 
<div class="columns large-12 small-12 medium-12"> 
</div> 
<p id="notice"><%= notice %></p> 

<h1>Listing Posts</h1> 

<table> 
<thead> 
<tr> 
    <th>Id</th> 
    <th>Title</th> 
    <th>Body</th> 
    <th>Body</th> 
    <th>Body</th> 
    <th>Body</th> 
</tr> 
</thead> 
    <tbody> 
    <%= @posts.each do |post| %> 
    <tr> 
    <td> <%= post.id %>  </td> 
    <td> <%= post.title %>  </td> 
    <td> <%= post.body %>  </td> 
    <td > <%= link_to 'Show' %> </td> 
    <td > <%= link_to 'Edit', edit_post_path(post) %> </td> 
    <td> <%= link_to 'Destroy', post, method: :delete %> </td> 

    </tr> 

    <% end %> 
    </tbody> 

</table> 
<%= link_to 'NEW POST', new_post_path %> 


</div> 

它除了@posts对象的形式打印在下面一样只是在我的浏览器table标签上面渲染罚款。我知道这是愚蠢的,但我无法弄清楚。

[#<Post id: 4, title: "new title", body: "this is new body", created_at: "2015-12-21 07:44:42", updated_at: "2015-12-21 10:31:31">, #<Post id: 7, title: "new title", body: "dfdsfd", created_at: "2015-12-21 09:40:01", updated_at: "2015-12-21 09:40:01">]

回答

0

海只是删除 '=' 在@ posts.each线。 <%@posts.each do | post | %>,就是这样。

<div class="row"> 
 
<div class="columns large-12 small-12 medium-12"> 
 
</div> 
 
<p id="notice"><%= notice %></p> 
 

 
<h1>Listing Posts</h1> 
 

 
<table> 
 
<thead> 
 
<tr> 
 
    <th>Id</th> 
 
    <th>Title</th> 
 
    <th>Body</th> 
 
    <th>Body</th> 
 
    <th>Body</th> 
 
    <th>Body</th> 
 
</tr> 
 
</thead> 
 
    <tbody> 
 
    <% @posts.each do |post| %> 
 
    <tr> 
 
    <td> <%= post.id %>  </td> 
 
    <td> <%= post.title %>  </td> 
 
    <td> <%= post.body %>  </td> 
 
    <td > <%= link_to 'Show' %> </td> 
 
    <td > <%= link_to 'Edit', edit_post_path(post) %> </td> 
 
    <td> <%= link_to 'Destroy', post, method: :delete %> </td> 
 

 
    </tr> 
 

 
    <% end %> 
 
    </tbody> 
 

 
</table> 
 
<%= link_to 'NEW POST', new_post_path %> 
 

 

 
</div>

+0

它有时会发生,当我们是新的轨道。 – Sravan

1

原来这是一件真的傻了! erb模板引擎需要=erb标记才能显示数据,而不使用=来编写逻辑。

这只是这样一个简单的变化:

<% @posts.each do |post| %> # without the = symbol