0

Dumb stackoverflow asks for more context... I open the index post page where I do (below) and for some weird reason an extra array appears beneath the actual texts. WHY?

<h1><%= @posts.each do |post| %></h1>
<p><%= post.content %></p>
<% end %>

POSTS CONTROLLER:

class PostsController < ApplicationController

  def index
    @posts = Post.find(:all, :order => "content")
  end

  def new 
    @post = Post.new
  end

  def create
      @post = Post.new(params[:post])
    end
  end

USER MODEL:

class Post < ActiveRecord::Base
  attr_accessible :content, :title
  belongs_to :user
end

Ayy ideas, I displays like this.

Yippi

[#<Post id: 1, title: "Hello", content: "Yippi", created_at: "2013-01-25 16:36:08", updated_at: "2013-01-25 16:36:08">]

2 Answers2

3

<h1><%= @posts.each do |post| %></h1> should read <h1><% @posts.each do |post| %></h1>

when you are using <%= %>, it's output will be in your erb file. If you were to type posts.each in your console, you would see that the return statement would pe the array of posts. You are outputting that array to your erb because you are using <%= instead of <%

This post has little bit more info around ERB sytanx: What is the difference between <%, <%=, <%# and -%> in ERB in Rails?

Community
  • 1
  • 1
Iuri G.
  • 9,670
  • 3
  • 20
  • 37
0
<% @posts.each do |post| %>
  <tr>
    <td><%= post.title %></td>
    <td><%= post.content %></td>
    <td><%= post.created_date %></td>
    <td><%= post.updated_date %></td>
   </tr>
<% end %>