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 Build a Simple Ruby on Rails Application Customizing Forms Creating Relationships

undefined method `full_name' for nil:NilClass

I am having issues with an error around the full_name as well as first_name and last_name

2 Answers

Jamie McCaw
Jamie McCaw
5,579 Points

You can go into the rails console and check record(s) you're trying to get first_name and last_name from and make sure they aren't nil. An example of this inside the rails console would be, if looking for instance at a users record:

user = User.first  # This will set user equal to the first user in your records
user.first_name # Checking whether that user has a first_name field and what it is
user.last_name # Same as above but with the last name
user.first_name = "The users first name" #This would set the users first name if it's empty and the field exist, can be done with last name as well
user.save #This saves the user to the DB 
Jamie McCaw
Jamie McCaw
5,579 Points

Whatever you are trying to pass into or call 'full_name' on is nil when you are doing it. An example of what this would look like is:

def full_name 
  first_name + " " + last_name
end

If you never set what first_name or last_name are, calling the full_name method will give you the same error, you may also want to check your database and make sure what you are calling have a last_name and first_name or those methods will give a similar error.

tl:dr: 'for nil:NilClass' usually means you're trying to do whatever you're doing on nothing.

heycynwrites
heycynwrites
20,022 Points

Hi, I am having the same issue as well. Do I need to fix this issue via the rails console? What command do I need to type?