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
Unsubscribed User
8,167 PointsError for undefined method 'underscore'
Anyone know how to fix this error?
undefined method `underscore' for nil:NilClass
Working in the Displaying Activity section of the ruby project and getting this error when reloading the activity page
Here is where it's used on the index.html.erb page of the statuses
<%= render partial: "activities/#{activity.targetable_type.underscore}/#{activity.action}",
locals: { activity: activity } %>
Here are my files on github https://github.com/licatajustin/treebook
6 Answers
Patrick Cooney
12,216 PointsIt appears in my not at all expert opinion, based on the error, that you're trying to call underscore on the class nil. Are you sure the class is initialized and everything is spelled correctly? To be perfectly honest I'm not really a ruby person but I've worked with a couple of OO languages and when I've had errors like this it has come from making calls to uninitialized objects.
I could be wrong, it could be ruby specific, but it looks like a generic OO bug.
Unsubscribed User
8,167 PointsThank you for the reply Patrick, I'll go back and check for misspelling and so forth.
Unsubscribed User
8,167 PointsHere is the error I'm getting
1) Error:
test_should_get_index(ActivitiesControllerTest):
NoMethodError: undefined method `friends' for nil:NilClass
/Users/justinlicata/Documents/ruby/gnaarly/treebook/app/controllers/activities_controller.rb:3:in `index'
Patrick Cooney
12,216 PointsSorry man, as I said I'm none too good with Ruby. I'm a Java and Obj-C guy. I haven't had a chance to dive into the Treebook project yet so I'm afraid I can't offer up any other suggestions. I'm sure someone around here will know what's up. More importantly than spelling though check for initialization of objects.
alex morrison
135 Pointsi have the same problem
Leo Brown
6,896 PointsI am also having this problem. Activities are created, but the targetable_type does not get set. So the Activity exists, can be displayed in the view, etc., but when I try to do something with the targetable_type, it of course fails.
Here is what an Activity looks like once it is created (in the console):
<Activity id: 2, user_id: 17, action: "created", targetable_id: nil, targetable_type: nil, created_at: "2015-12-08 05:16:46", updated_at: "2015-12-08 05:16:46">
Activities controller:
class ActivitiesController < ApplicationController
def index
following_ids = current_user.following.map(&:id)
@activities = Activity.where("user_id in (?)", following_ids.push(current_user.id)).order("created_at desc").all
end
end
Activity Model:
class Activity < ActiveRecord::Base
belongs_to :user
belongs_to :targetable, polymorphic: true
end
An example of an Activity creation in another controller:
def create
@list = current_user.lists.build(list_params)
if @list.save
current_user.create_activity(@status, 'created')
redirect_to root_url
flash[:success] = "Your grocery list is being created."
else
render 'new'
end
end
I am midway through the Displaying Activity video. I appreciate any help, thank you!
Leo Brown
6,896 PointsI just discovered the reason this was failing for me. in my controller action (final code snippet), I have @status (as it is in the video) instead of @list, which is what I am working with in my app. Probably half of my errors are due to something like this. Anyway, glad to find it, and best of luck to everyone else!