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

It keeps saying: undefined method `body' for #<Post:0x007ff0bc7b78c0>

I did exactly as in the video, don't know what's wrong.. please help ^^

1 Answer

Samuel Ferree
Samuel Ferree
31,722 Points

Can you show me the code for Post.rb and the migration that creates the post table?

it's not finding the body method/property on your Post model.

This is where it's giving me the error. Is this what you ment? It's the Index.html.erb

<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 %>

This is the post.rb

class Post < ApplicationRecord
end
Samuel Ferree
Samuel Ferree
31,722 Points

The error is thrown when the view (index.html.erb) is rendered, but it exists because your post model doesn't have a body property. This could be because it doesn't have a column named body in the database, or what your controller is passing to the view for some reason doesn't have one.

What I see here looks fine, can you post your PostsController.rb and your Post migrations?

Samuel Ferree
Samuel Ferree
31,722 Points

Can you also show me the terminal output from the following commands?

rails c
Post.first
Post.all