0

I am just starting out with Ruby on Rails and to practice, I am creating a simple blogging app. On the page I created to view the list of all posts, all the data that is passed to the page is being rendered in an array near the top of the page like so:

enter image description here

I can't figure out why the array is showing. Here's the controller for that page:

def index
    @posts = Post.all
end

And heres the view:

<h1>Listing posts</h1>
<table>
<tr>
    <th>Title</th>
    <th>Text</th>
</tr>

<%= @posts.each do |post| %>
    <tr>
        <td><%= post.title %></td>
        <td><%= post.text %> </td>
    </tr>
<% end %>
</table>

and for the route I have tried both:

get '/posts/(.:format)' => 'posts#index'

and

get '/posts' => 'posts#index'

Also as a side question, what does (.:format) do anyway? The page seems to work the same whether I include that in the route or not.

1 Answer 1

4

When you use an "=" sign in the view it renders the result in html:

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

Change the above line to:

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

The format is only useful when you want to render multiple formats (default is html), but you could call the page with a ".xls" or a ".json" etc and then have code in your controller to respond to those formats.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.