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

Fardeen Rahaman
Fardeen Rahaman
614 Points

Undefined Method 'First Name' Error

Hi, I reached the time frame 5:45 of the chapter Creating Relationships where Jim Hoskins specifies adding the '''<%= @status.user.first_name %>''' on line 5 of the file /app/views/statuses/show.html.erb.

As per the video, Jim is able to see the appropriate user's name appear upon refreshing the page. However, I am receiving the error message as:

'''Showing /Users/fr/Documents/Coding/Treehouse/RoR/treebook/app/views/statuses/show.html.erb where line #5 raised:

undefined method `first_name' for nil:NilClass'''

Appreciate the help.

20 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Fardeen,

Is first_name listed as attr_accessible in your User model class?

Fardeen Rahaman
Fardeen Rahaman
614 Points

Hi Justin, yes. this is already listed.

I just found out that in /app/controllers/statuses_controller.rb, the show method was empty. I defined it in the following way:

def show @status = Status.find(params[:id]) end

However, the issue still persists.

Fardeen Rahaman
Fardeen Rahaman
614 Points

I searched on the web and found out that this issue usually happens when the database is not updated (or migrated).

The only difference in my steps as compared to what's shown in Jim Hoskins' video is that I did not run the "rake db:migrate".

It did not make any sense to me as I am the sole collaborator on the project on GitHub. When I checked the "git pull" output, it showed me that all my files are up to date.

Hence, I don't suspect an issue with database migration.

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Seems like a lot of people are having issues with this. So the problem is not related to first_name but rather, that it is being called on a null object. Which means the user object on @status is null.

Have you tried checking the database to make sure the user object is being saved to a status as expected? If the user isn't getting saved as it should it will always return null for the user and thus, try to call first_name on it and get this exception.

I would recommend deleting the test data created prior to these changes and create a new status. It's possible this error is coming from old data before the status object had the user relationship.

Hope this helps.

Fardeen Rahaman
Fardeen Rahaman
614 Points

Hi Justin, I followed your instructions. I deleted all the test data as well as the users which I created. I then verified the output from rails console below:

2.0.0-p247 :001 > User.all User Load (2.2ms) SELECT "users".* FROM "users" => #<ActiveRecord::Relation []>

I then created a test user (mikey) and added a status update. I then verified the output from rails console below:

.0.0-p247 :006 > User.all User Load (0.2ms) SELECT "users".* FROM "users" +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+ | id | fir | las | pro | ema | enc | res | res | rem | sig | cur | las | cur | las | cre | upda | +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+ | 5 | | | | mik | $2a | | | | 1 | 201 | 201 | 127 | 127 | 201 | 2014 | +----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+------+ 1 row in set

I verified the status page is working fine. The moment I added the .first_name back again, it gave me the same error.

Can't figure out what's going on!!

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Okay you might want to check from the status in the database as well. Make sure that the user id for mikey is being stored in the database for the status you create.

I'm going to tag Jason Seifer and Jim Hoskins. Hopefully we can get some more help for you on this.

Fardeen Rahaman
Fardeen Rahaman
614 Points

Hi Justin, I think I figured out the issue. I was trying to create a status of a user that doesn’t exist. I needed to provide the ID of the user that already exists. In this case, its 5 and it works. But I still don't see the user's first name appearing.

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Fardeen,

I'm glad we're making progress. So now we know for sure that your status is being linked to a user. My previous suggestion was to ensure exactly that by taking note of the user id (5) and checking the Status record in the database, much in the same way you checked for User records, and make sure that the user id was being set to the user id of 5.

Now that we're past that you're still not seeing the first name display in the UI. Looking at the output you provided it seems as though the first name field is actually null or an empty string. I might be looking at it wrong, but can you confirm this?

From the output it seems as though mikey is set as the last name instead of the first name.

Fardeen Rahaman
Fardeen Rahaman
614 Points

Hi Justin, I checked the link and noticed Randy Wang's suggestion (shown below between). Where should I be adding this code?


**By adding the following code I'm now able to store the "first_name" "last_name" and "profile_name" in the database which I can verify by looking up the user in the Rails Console:

before_filter :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name, :user_id) } end

However, @status.user continues to be "nil" and thus I'm unable to call "first_name" on @status.user. When I create a new status, I get "Unpermitted parameters: user_id" in the console log and when entering the user_id in the new or edit status forms, the user_id is not being saved like in the video lesson.**

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Try placing that code in the ApplicationController.

Fardeen Rahaman
Fardeen Rahaman
614 Points

I tried already. It gave me an error upon refreshing the page.

I cannot attach snapshots, so I am copying and pasting the error here:

SyntaxError in StatusesController#index

/Users/fr/Documents/Coding/Treehouse/RoR/treebook/app/controllers/application_controller.rb:10: syntax error, unexpected '.', expecting ';' or '\n' ...ers devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(... ... ^ /Users/fr/Documents/Coding/Treehouse/RoR/treebook/app/controllers/application_controller.rb:12: syntax error, unexpected keyword_end, expecting end-of-input

Extracted source:

class StatusesController < ApplicationController before_action :set_status, only: [:show, :edit, :update, :destroy]

Application Trace: app/controllers/statuses_controller.rb:1:in `<top (required)>'

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

With the code added does your ApplicationController look like this?

class ApplicationController < ActionController::Base
before_filter :configure_permitted_parameters

  protected

  def configure_permitted_parameters 
      devise_parameter_sanitizer.for(:sign_up) do |u| 
         u.permit(:email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name, :user_id)
       end
  end
end

In the linked thread Maciej Czuchnowski stated that the code was added in the ApplicationController so I believe we have the right file. We just have to find out why it's giving you the error it is.

Fardeen Rahaman
Fardeen Rahaman
614 Points

Yes, its the same. I have pasted the output here:

'''class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception

before_filter :configure_permitted_parameters

protected

def configure_permitted_parameters devise_parameter_sanitizer.for(:sign_up) do |u| u.permit(:email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name, :user_id) end end

end'''

I just tried it again and this time I get the following errors (the code in asterisk is highlighted as the main cause of error):

NameError in StatusesController#index undefined local variable or method `resource_class' for #<StatusesController:0x007f98ac890e08>

Extracted source (around line #11):

def configure_permitted_parameters *devise_parameter_sanitizer.for(:sign_up) do |u| * u.permit(:email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name, :user_id) end end

Rails.root: /Users/fr/Documents/Coding/Treehouse/RoR/treebook

Application Trace | Framework Trace | Full Trace app/controllers/application_controller.rb:11:in `configure_permitted_parameters'

Brandon Barrette
Brandon Barrette
20,485 Points

What is the output in the rails console for

User.find(5)
Fardeen Rahaman
Fardeen Rahaman
614 Points

Hi Brandon, here is all the output. (P.S.: The output appears messed up when posting on the forum, hence, I am deleting unnecessary columns)

2.0.0-p247 :004 > User.all User Load (0.3ms)
SELECT "users".* FROM "users" +----+-----+-----+-----+-----+-----+--- | id | fir | las | pro | ema | enc | res | +----+-----+-----+-----+-----+-----+--- | 5 | | | | mik | $2a | | |
| 6 | | | | far | $2a | | |
| 7 | | | | sam | $2a | | | | 8 | | | | leo | $2a | | |
+----+-----+-----+-----+-----+-----+--- 4 rows in set 2.0.0-p247 :005 > User.find(5) User Load (5.5ms)
SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 5]] +----+-----+-----+-----+-----+-----+--- | id | fir | las | pro | ema | enc | res | +----+-----+-----+-----+-----+-----+--- | 5 | | | | mik | $2a | | |
+----+-----+-----+-----+-----+-----+--- 1 row in set

Brandon Barrette
Brandon Barrette
20,485 Points

So a few things I noticed. The columns in your database say "fir" for first_name, "las" for last_name, and "pro" for profile name. Is this because they are just not copying correct? Or is that how you entered them in your database?

The columns in the database have to matchup with the method you're calling. So if you call:

status.user.first_name

Rails is going to look for the user on that status and then look for the first_name column. Since full name is a method on user, it looks for first_name and last_name.

It also appears that the names on your users are not making it into the database. This suggests an error with devise.

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

He has fixed the link between status and user. The initial issue was caused by the user object on status being nil. Now that problem is solved but he still doesn't see the first name display in the UI.

We're now trying to figure out why devise is not inserting the values for first_name, last_name and profile_name to the database.

You make a great point about the column names. If they column names in the database (first_name, last_name, profile_name) don't match what devise is expecting then it's impossible for devise to perform the insert correctly.

Tzevai Chong
Tzevai Chong
15,903 Points

Hi guys, I was having the same problem where my first_name, last_name weren't storing in the db so I pasted the code above in the application_controller.rb file and had the same "resource_class" error.

I googled it and found this link which states to amend the before_filter line to:

before_filter :configure_permitted_parameters, if: :devise_controller?

Once I did that the first_name, etc were stored correctly in my database (verified in the console)