Mark Khmall
Mark Khmall

Reputation: 37

Newbie Q:showing data from db in view

Im just starting with RoR. I've been following a 10m guide on how to make a blog (link). Here's my problem, code from one of the views

<% @posts.each do |post| %>
<h2><% link_to post.title, post %></h2>
<p>
  <% time_ago_in_words post.created_at %> ago
</p>
<p>
  <% truncate post.text %>
</p>
<% end %>

Post was created using scaffold with title and text being both text. It looks like nothing is getting retrieved from database, neither title, nor text. This is how the page looks in browser (with 2 posts added to the db):
Welcome to my blog
ago
ago

Not sure what could be the cause of this. Using

Upvotes: 0

Views: 33

Answers (1)

Danil Speransky
Danil Speransky

Reputation: 30453

Use <%= not <%. It means you cast the value to string and put it into html. With <% you just do some stuff.

<% @posts.each do |post| %>
  <h2><%= link_to post.title, post %></h2>

  <p>
    <%= time_ago_in_words post.created_at %> ago
  </p>

  <p>
    <%= truncate post.text %>
  </p>
<% end %>

Upvotes: 2

Related Questions