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

NoMethodError in Statuses#Index

Hey All,

I am trying to follow along with the videos and create a full_name method. I keep getting the error "NoMethodError in Statuses#Index" and a little under that it says "undefined method `full_name' for nil:NilClass". I have a feeling it's something really simple that I'm missing.

Code: (The method call is on the 3rd line, "status.user.full_name")

<% @statuses.each do |status| %>
  <div class="status">
    <strong><%= status.user.full_name %></strong>
    <p><%= status.content %></p>
    <div class="meta">
      <%= link_to time_ago_in_words(status.created_at) + " ago", status %>
      <span class="admin">
        | <%= link_to "Edit", edit_status_path(status) %> | 
        <%= link_to "Delete", status, method: :delete, data: {confirm: "Are you sure you want to delete this status?"} %>
      </span>
    </div>
  </div>
<% end %>
class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  has_many :statuses

  def full_name
    first_name + " " + last_name
  end
end

1 Answer

I'm no expert, but could it be that you should include "full_name" under "attr_accessible" so the method is, well, accessible? I had a look in my user.rb, and that's how it looks there at least. Here's a link if you wanna compare with your own

Hope it helps :)

-M

I'm using Rails 4 and don't have the attr_accessible gem. I have been using strong params which are included in my users controller. It's been working fine for the most part, until he created the full_name method. It looks like it's just not seeing the method, unless I'm doing something else wrong.

Oh my bad, I didn't catch that you were using 4. Have you tried deleting the existing statuses in you app to make sure you don't have any old statuses from before you implemented first_name, last_name, full_name etc.?