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
Brandon Trujillo
Courses Plus Student 12,656 PointsStuck in Rails Development Stage 4 Creating Relationship Treebook
Hi there, I have a problem with passing my first_name and last_name arguments and keep getting this error
undefined method `first_name' for nil:NilClass Extracted source (around line #9):
''' <% @statuses.each do |status| %> <div class="status"> <strong><%= status.user.first_name %></strong> <p><%= status.content %></p> <div class="meta"> <%= link_to time_ago_in_words(status.created_at) +" ago", status %> '''
Im using rails 4 i'm still new to rails. Plz HELP!! Thankyou
6 Answers
Christo Steyn
1,849 PointsI have to say this worked for me: Inside application_controller.rb:
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) }
end
end
then I dropped, recreated and migrated my db
rake db:drop
rake db:create
rake db:migrate
deleted all statuses in console
rails console
Statuses delete_all
exit
ran the server
rails server
signed in at /user/sign_up
then only after creating a few statuses did both views work for me. Status>Show and Status>Index.
samschlinkert
2,696 PointsNot an expert, but what's probably happening is one or more of your statuss does not have a user associated with it. So, at least once, the first_name method is being called on nil (because status.user returned nil in this case), thus causing the error.
One fix MIGHT be to use <%= status.user.first_name if status.user %>
Roberto Alicata
Courses Plus Student 39,959 PointsYou are using Rails 4, so you have to add this code to the app/controllers/application_controller.rb to ensure that you can save the user's parameters like first_name, last_name etc.
before_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) }
end
Brandon Trujillo
Courses Plus Student 12,656 Pointsawesome thank you that helped a lot.
Brandon Trujillo
Courses Plus Student 12,656 PointsDid all that and I'm back to square one, its seems like it doesnt know how to display my name but my name is registered when I sign up its in there even shows up when I creted a drop down menu. I been stuck for hours even remigrated the db a few times.
Heres my user.rb
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible for protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name
# attr_accessible :title, :body
has_many :statuses
def full_name
first_name + " " + last_name
end
end
application controller.rb
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_action :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:first_name, :last_name, :profile_name, :email, :password, :password_confirmation) }
end
end
Roberto Alicata
Courses Plus Student 39,959 PointsTry to comment out all the attr_accessible in the user.rb.
If you want you can look at my code at github (I'm a little bit further in the course)
Can you dump here the rails server response when this error appear?
Roberto Alicata
Courses Plus Student 39,959 PointsRoberto Alicata
Courses Plus Student 39,959 PointsI remember I also had deleted all the data in the Status table because many records were set to nil.