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
Rodrigo Hooli Coolio Esquire
5,309 PointsRuby on Rails Treebook — Customizing Forms:Creating Relationships Error
Hello,
The tutorial was going fine until the 5:50 mark in the Creating Relationships when adding the @status.user.first_name to show.html.erb and the index.html.erb and later changing to <%= @status.user.full_name in both has seemingly nuked the entire app with the following error: NoMethodError in Statuses#index Showing c:/Users/Michael/RubymineProjects/treebook/app/views/statuses/index.html.erb where line #9 raised: undefined method `full_name' for nil:NilClass Extracted source (around line #9): 6: 7: <% @statuses.each do |status| %> 8: <div class="status"> 9: <strong><%= status.user.full_name %></strong> 10: <p><%= status.content %></p> 11: <div class="meta"> 12: <%= link_to time_ago_in_words(status.created_at) + " ago", status %>
app/views/statuses/index.html.erb:9:in block in _app_views_statuses_index_html_erb___474621131_47349792'
app/views/statuses/index.html.erb:7:ineach'
app/views/statuses/index.html.erb:7:in _app_views_statuses_index_html_erb___474621131_47349792'
app/controllers/statuses_controller.rb:7:inindex'
I was since went through the project files code to verify that there were no typo’s. I tried adding a new user which would have an ID for posts and I went through the rails console and deleted all users to flush out any posts that would not have ID’s. I also went to the Treebook Github for commits for this lesson and did not see anything in the code that is different. The only page that seems to work in the browser at this point is the Sign up page.
Could someone please advise if they have any insight into this?
Thanks
7 Answers
Tulio Cardozo
Courses Plus Student 588 PointsA different approach/
I explored a bit of the Action Controller:Exception page that came up when I hit the "NoMethodError in Statuses#show" It was there that I noticed a way to get the id for the user. By clicking the "show session dump" link towards the bottom I found the following:
warden.user.user.key: ["User", [4], "$2a$10$6ncjHnBWUS8uHVO65e1/xe"]
When following the video I assumed that my user_id was 1, in-fact it was 4. By using that value (4) where I had previously used 1, the errors went away.
I'll still be checking out sqlitebrowser though ;-)
-Thanks!
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.
Dave Faliskie
17,793 PointsThank you so much! this took me awhile to figure out. and that ^ worked
nofx1717
5,096 PointsSo remove those lines from show, and index. At that point delete all your post you had before them. Once you delete all your pots before that re-add those lines and see if it works.
Rodrigo Hooli Coolio Esquire
5,309 PointsOh Teh, after a few hours of frustration I think I just got through my error. I'll post this for reference to anyone else with the same issue as it seems it's common for this tutorial.
According to a response from the instructor Jim, on another user's entry:
"NoMethodError in the case of models is usually a symptom of the database not having the right columns. Since you didn't have the method :user_id, and it was a column we were adding at that point in time, I would guess that you may have forgot to migrate the database, or the migration may have failed the first time around."
I also ran rake db:migrate again and it did not solve the error. I have already gone through the Michael Hartl RoR tutorial and we had used SQLite Database Browser. sqlitebrowser.sourceforge.net/ (I'm using 2.0 but some have to use 1.1 if you run into trouble.) In taking a look at the database information I wanted to see if all of my columns were there. [I apologize but apparently you can't paste images into this forum.]
After opening Treebook's development.sqlite3 file with the browser software I saw the right columns but in clicking into the ["Browse Data" tab : Table drop down :statuses] I could see that the user_id cell was 0. I figured this was the fraking nil that was causing the error so I manually changed it in SQLite Database Browser to 4 to match the id cell count.
Then I saved and went right to check the site and I could get back to all the pages that were crashing before making the change. There's probably a way to fix this through the Rails Console but if you're receiving nil errors this could be why. Also SQLite Database Browser is a handy tool for visual checks of what's sitting in the dev database.
Hope this helps.
Namaste
Tanner Brandt
12,044 PointsThank you Michael! That was so frustrating, I spent 4 hours trying to figure this error out until I found this post. For anyone who is having the same problem - you might have done what we did and created/destroyed multiple users on your site. Download the SQLite Database browser first: http://sourceforge.net/projects/sqlitebrowser/
Then use the browser to open up your development.sqlite3 file within your app (within your db folder).
Go to the 'Browse Data' tab. Then switch between the tables in the drop down menu. There you can manually change what is in the user_id column within the Statuses Table to match your id in the Users table.
Sean Alaback
3,561 PointsTulio --- this method saved me! I rather wish Treebook would link forum comments to the videos so I could quickly find how people have screwed up during a step and gotten themselves out of it.
John Moon
3,130 PointsTulio --
I'm sort of lost. I found the warden key and for me it says 2. How do I change that to 1?
Jesse Waites
146 PointsHELP!
Daniel Williams
378 PointsDaniel Williams
378 PointsNice one Tulio!
John Steer-Fowler
Courses Plus Student 11,734 PointsJohn Steer-Fowler
Courses Plus Student 11,734 PointsFor others needing a quick fix of these errors - READ THIS COMMENT FIRST!
This saved me hours of trial and error! Thanks a lot Tulio!
ps. Treehouse - would be nice if you could somehow highlight useful posts like Tulio's