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
Stephen Whitfield
16,771 Pointsundefined method `full_name' for nil:NilClass
For some reason, this error keeps coming up when I try to load my index file.
Here's my status.rb:
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name
has_many :statuses
def full_name
return first_name+' '+last_name
end
end
Index.html file:
<div class="page-header">
<h1>Friend Statuses</h1>
</div>
<%= link_to "Post a New Status", new_status_path, class: "btn btn-success" %>
<% @statuses.each do |status| %>
<div class="status">
<strong><%= status.user.full_name %></strong> <!-- bold -->
<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?"} %>
</span>
</div>
</div>
<% end %>
7 Answers
Josh Flowers
7,010 PointsYou didn't state your error.
My question is which version of Ruby and Rails are you using? If you're using Ruby 2.x, then the attr_accessible won't work as it's been dropped from the language due to security concerns.
So, if you're using Ruby 2.x and Rails 4.x then you're going to keep running into problems. Treehouse is currently working on updates for these new versions, so my advise would be to take a break and learn something else until they release it, or start over using the same versions of Ruby and Rails that they use.
Cheers.
Michael Wiss
19,233 Pointswaiting for that update.
Josh Flowers
7,010 PointsI got an email for Jason Seifer two days ago saying that the Rails 4 version will come out soon.
Michael Wiss
19,233 Pointshuzzah!
Josh Flowers
7,010 PointsAlthough the word "soon" is quite relative.
Michael Wiss
19,233 PointsHave you tried the Lynda.com course?
Josh Flowers
7,010 PointsNo, I thought it was also on Rails 3? There's a good free book http://ruby.railstutorial.org/ruby-on-rails-tutorial-book which is written by the creator or Rails and it's on Rails 4. It's very good and comes highly recommended.
Stephen Whitfield
16,771 PointsHave any of you two tried moving on with that portion of the tutorial by referring back to the docs and learning it yourselves? I just might do that tonight. Figure it's better practice to sort of teach yourself concepts without having someone else do it for you.
Michael Wiss
19,233 PointsYes. I made some progress after using
'''#use attr_accessible in ruby 4 gem 'protected_attributes' '''
but ran into more setbacks.
Michael Wiss
19,233 PointsYes I used this gem:
#use attr_accessible in ruby 4
gem 'protected_attributes'
but ran into more setbacks.
Stephen Whitfield
16,771 Pointsdarn. maybe it's best to wait until they update it after all. guess i'll move on to other modules.
Jeff Wilson
Courses Plus Student 25,216 PointsI ran into the same problem last week and since reading about the changes they've made in Rails 4 I think it's worth understanding the change and seeing if you can make it work.
Basically as of Rails 4 attr_accessible has been removed and they want you to use Strong Parameters method as it allows for a better separation of concerns by moving the rules to the controller rather than the model. It's also better from a security standpoint.
I got my code working by doing the following: 1) I added a private class in the statuses controller that follows the strong parameters method
'' 'ruby def status_params params.require(:status).permit(:date, :content).merge(user_id: current_user.id) end '''
2) I updated the the status's model to include ''' ruby accepts_nested_attributes_for :User ''' which establishes the relationship for the strong parameters
You can read about strong parameters here: http://robots.thoughtbot.com/strong-parameters-as-documentation http://weblog.rubyonrails.org/2012/3/21/strong-parameters/
BTW - strong parameters exist in Rails 3 so you can try them out regardless of which version you're running.
Stephen Whitfield
16,771 PointsAwesome! Thanks Jeff.
minzawmra
Courses Plus Student 15,682 PointsFound a solution here: http://gorails.com/blog/rails-4-0-and-devise-with-strong-params-and-custom-fields
It works!
Stephen Whitfield
16,771 PointsThanks! I'll give it a shot.
Brandon Barrette
20,485 PointsSo the error isn't that full_name is not defined. The error is that you are trying to call full_name on Nil, meaning there is no user defined. My guess is that the user_id is not being stored with the status in the database. Like Josh Flowers said, using Rails 4 with these tutorials is going to give you constant headaches. I would learn using Rails 3.2 and worry about first learning how Rails works, than trying to use the most recent (and still very new) version.
Stephen Whitfield
16,771 PointsHey Brandon. Thanks for the reply. I have already taken Josh's advice. My intention wasn't to install the latest version of Rails--it was to install Rails as per the instructions supplied (sudo gem install rails). It was by chance that the latest version was the one that was installed. Now that I know that the latest version doesn't support attr_accessible, I know that my understanding of Rails isn't the problem.
Michael Wiss
19,233 PointsI installed RVM and now have multiple versions of Rails to use. I found this useful
Stephen Whitfield
16,771 PointsThanks for the heads-up! From the reviews, it looks like a life-saver.
Steven Lacks
18,214 PointsThis should actually work in rails 4/4.1, the problem for me wasn't a version issue. If the tests work, this should work. It's because we previously went in and updated friends before the models/controllers were finished. I used SQLite3 database browser to delete all the incomplete entries and voila! it started working even in 4.1
I still recommend doing it in the correct version if possible. RVM is cool.
Stephen Whitfield
16,771 PointsStephen Whitfield
16,771 PointsThe error is the title of my post. Thanks!