Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby Ruby on Rails 5 Basics Adding a Model Attribute Updating Views

NoMethodError in Posts#index

I don't know what's wrong with my code I did exactly the same as in the video, Please help ^^

This is my code, it keeps saying "undefined method `body' for #<Post:0x007fe5b10529b0>"

========================================================================

<p id="notice"><%= notice %></p>

<h1>Posts</h1>

<table>
  <thead>
    <tr>
      <th>Title</th>
      <th>Body</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @posts.each do |post| %>
      <tr>
        <td><%= post.title %></td>
        <td><%= post.body %></td>
        <td><%= link_to 'Show', post %></td>
        <td><%= link_to 'Edit', edit_post_path(post) %></td>
        <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Post', new_post_path %>

========================================================================

Ryan Bent
Ryan Bent
6,433 Points

Can you show me your Post controller?

1 Answer

Jay McGavren
STAFF
Jay McGavren
Treehouse Teacher

Hi Sean Cedric Baart ,

Since Post is a model class, it will not have a body method unless there is a body column in the database. Did you create (and then run) a migration to add a body column to the posts table?