新手问题 新手做 Blog,index 页面底部出现问题

jojoma · 2015年11月17日 · 最后由 jojoma 回复于 2015年11月17日 · 1500 次阅读

我跟随教程做 blog,其中 views/posts/index.html.erb 文件内容如下

<%= @posts.each do |post| %>
  <div class="post_wrapper">
    <h2 class="title"><%= link_to post.title, post %></h2>
    <p class="date"><%= post.created_at.strftime("%B, %d, %Y") %></p>
  </div>
<% end %>

但是打开浏览器,在 index 页面底部(上面正常显示 title 和 date),出现下面这样的语句,相当于把数据库内容列出来了:

---
[#<Post id:2, title:"this is a title", body:"body container", created_at:"2015-11-17 13:51:06", updated_at:"2015-11-17 13:51:06">, #<Post id:1, title:"new title", body:"some words", created_at:"2015-11-17 13:42:31", updated_at:"2015-11-17 13:42:31">]
---

可是我的 index 文件中,没有语句跟页面中以#<>形式列出数据内容的行为相关。我百思不得其解,搜索也不好搜,所以来请教一下

posts_controller 也只是写了一些简单方法,其中 index 方法是

def index
  @posts = Post.all.order('created_at DESC')
end

第一行那个等号不应该有

<%= @posts.each do |post| %>

改成

<% @posts.each do |post| %>

#2 楼 @torubylist 真的是这个问题...谢谢

#3 楼 @rei 恩,我改了就好了。谢谢。应该是这一行输出的结果

需要 登录 后方可回复, 如果你还没有账号请 注册新账号