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

Creating Relationships tutorial, error "NoMethodError in Statuses#new"

I was following the tutorial and I got the error on the http://127.0.0.1:3000/statuses/new (new status page) when I added the <%= f.input :user_id %> to my statuses/_form.html.erb file.

I want to add the method but I'm not sure where to put it. I'm open to suggestions!

27 Answers

Yeah I had the same problem and what I mentioned was what worked for me, so when you make a new status do you select the user then submit the status? Is that when you get the error?

I had the same problem. To resolve:

rails console
User.delete_all

Then sign up again as Hans pointed out.

I resolve with a Jason post:

Just do this on the console. (Will delete all posts and users)

rake db:drop:all rake db:create:all rake db:migrate

This worked for me

rake db:reset

rake db:migrate

Thanks Jim Bennett! That worked for me also, I've been sitting here scratching my head for an hour and that finally did it. :)

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Hey Allan, I'm not sure if this is the correct video based on your description but try following along with Jim in this video and see if that helps: http://teamtreehouse.com/library/programming-2/build-a-simple-version-of-facebook/customizing-forms/getting-the-latest-changes

Alright, I was following the tutorial and I think I found the problem, it didn't migrate the add users id to statuses file instead I get this message:

== AddUserIdToStatuses: migrating ============== -- add_column(:statuses, :user_id) rake aborted! An error has occurred, this and all later migrati

wrong number of arguments (2 for 3) C:/Sites/Projects/treebook/db/migrate/20130210171 3:in change' C:inmigrate' Tasks: TOP => db:migrate (See full trace by running task with --trace)

Says I have wrong number of arguments, is there something else I should have added the the file?

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Try changing the migration to say:

add_column :statuses, :user_id, :integer

Thanks that completed it, I'll go through the other lessons in that chapter to make sure everything else is good.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Great! Let us know if you need any more help :)

Feels like I made a big circle; I got a similar error, this time in the index.html.erb: NoMethodError in Statuses#index

Showing C:/Sites/Projects/treebook/app/views/statuses/index.html.erb where line #9 raised:

undefined method `full_name' for nil:NilClass Extracted source (around line #9):

9: <strong><%= status.user.full_name %>

Jim I think came across this problem in the video but instead of making a method he just deleted the other user ID's from the statuses page, but I'm not able to access the statuses page. I'm sure it's something simple I'm just having a hard time seeing it.

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

Try opening up your Rails console by doing the following:

bundle exec rails console

Then once you're in there, try this:

Status.delete_all

That will delete all of your statuses (which will also include the ones without a user id). From there you should be able to recreate them.

Alright I did that and when I deleted the statuses I was able to access the status page, but I still have the problem that if I add a status I get the previous error of the full_name not being defined. Should I continue the chapter or re download the chapters files?

Hi,

I'm having the same problem. I followed your instructions and I was able to access the status page again, but I'm not able to post a new status update. This is my error:

NoMethodError in Statuses#show

undefined method `full_name' for nil:NilClass Extracted source (around line #5):

4: <b>Name:</b> 5: <%= @status.user.full_name %>

Hi,

Also have a similar issue.

undefined method `user' for nil:NilClass

From index.html.erb:

<% @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 %> 

Have tried removing all entries from the database as suggested, it allowed me to see the main page but the issue reappeared one I attempted to create a new user.

Okay, for me the issue was as follows:

app > views > statuses

<strong><%= @status.user.full_name %></strong>

should be:

<strong><%= status.user.full_name %></strong>

note the lack of '@'.

I had the same problem as Allan in the index.html.erb: NoMethodError in Statuses#index

Showing C:/Sites/Projects/treebook/app/views/statuses/index.html.erb where line #9 raised:

undefined method `full_name' for nil:NilClass Extracted source (around line #9):

9: <%= status.user.full_name %>

I applied Jason's method, and it worked !!

Thanks Jason :°)

PS : since so many seem to get stuck at this stage, any possibility to add a comment below the video ?

Ok i got it finally, i had the drop box with the numbers displaying under the drop box with the names in the "new" user page, i got rid of that and it displayed fine. Must have forgotten to remove that.

Hey Allen could you please explain how you fixed your problem a little more? Thanks!

Sure, in the app\views\statuses_form.html.erb file I had a <%= f.input :user_id %> method that I deleted, because I had made the other method <%= f.input :user_id, collection: User.all, label_method: :full_name %> above it. That line caused me to have a second drop box for users in integers.

Oh I see, I must be stuck on a different problem then because I haven't even entered this yet: <%= f.input :user_id, collection: User.all, label_method: :full_name

I'm thinking it has something to do with the users, because every time I make a new post I get an error, which inhibits me from viewing the status. After posting a new post, I'm unable to view the status page (All of the Statuses) until I go into the console and delete them with Status.delete_all

Just wondering, would I have to delete my user for this to work, or am I missing something else? I could have sworn I was following the videos step by step ;-;

Yea, I'm getting an error when I enter that into my code, but I think I know what the problem is. I'm going to go and check the video to see if I messed up in the users.rb file! because this is my error,

NoMethodError in Statuses#new

Showing /Users/trevorwilliams/projects/Treebook/app/views/statuses/_form.html.erb where line #14 raised:

undefined method `+' for :first_name:Symbol Extracted source (around line #14):

14: <%= f.input :user_id, collection: User.all, label_method: :full_name %>

EDIT: I fixed it :)

I had an error in my users.rb file!

I am having a similar issue as well except when I click "post a new status" I get this error:

    NoMethodError in Statuses#new

    Showing /Users/simonpacheco/treebook/app/views/statuses/_form.html.erb where     line #16 raised:

    undefined method `name' for #<Status:0x007fcb2ade3ff8>
Extracted source (around line #16):

    13: 
    14:   <div class="field">
    15:     <%= f.label :name %><br />
    16:     <%= f.text_field :name %>
    17:   </div>
    18:   <div class="field">
    19:     <%= f.label :content %><br />
    Trace of template inclusion: app/views/statuses/new.html.erb

    Rails.root: /Users/simonpacheco/treebook

    Application Trace | Framework Trace | Full Trace
    app/views/statuses/_form.html.erb:16:in `block in     _app_views_statuses__form_html_erb__3892048837332768478_70255287655140'
    app/views/statuses/_form.html.erb:1:in   `_app_views_statuses__form_html_erb__3892048837332768478_70255287655140'
app/views/statuses/new.html.erb:3:in `_app_views_statuses_new_html_erb__2497411146972375420_70255287661700'
app/controllers/statuses_controller.rb:29:in `new'
Request

Parameters:

None
Show session dump

Show env dump

Response

Headers:

None

Anybody know why? Its driving me crazy!

Hi Simon

If I remember correctly your supposed to delete everything contained inside the div class 'field' and replace that with erb output tags. So you would have f.input :user_id & f.input :content & f.label would be replaced with f.button :submit.

It looks like you missed that step, that's probably why it's not working out. If your still having trouble check your index.html.erb file around line 9, there was a comment above by Pierre that addresses that issue too.

Just had this same problem but found the problem to be the following: I wasn't signed in with the account I selected the user ID from. My assumption was that I was signed in as user with user ID 1, while I was signed in with user ID 2. This gave me the error you guys are talking about. So a fix might be going to your rails console:

rails console
user = User.all
user.delete

After this go to localhost:3000/users/sign_up and sign up again. If you now make a new status with user ID 1, it should not display the NoMethodError anymore. Hope this helped :)

I have tried to clean statuses and users, but it did not fix the problem.

NoMethodError in Statuses#index

Showing /Users/sander/Documents/real/treebook/app/views/statuses/index.html.erb where line #8 raised:

undefined method `full_name' for nil:NilClass
Extracted source (around line #8):

5: 
6: <% @statuses.each do |status| %>
7: <div class="status">
8:   <strong><%= status.user.full_name %></strong>
9:   <p><%= status.content %></p>

10: <div class="meta"> 11: <%= link_to time_ago_in_words(status.created_at) + " ago", status %>

I 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.