Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby

Krishna Ramachandran
PLUS
Krishna Ramachandran
Courses Plus Student 5,559 Points

Error message in Build a Simple Ruby Application > Customizing Forms

I got the following error message in the Build a simple Ruby on Rails Application within the Customizing forms section. I can't figure out what I missed or what I need to define and where. Any help would be greatly appreciated!

NoMethodError in Statuses#show undefined method `user' for #<Status:0x00000101d31b78> Extracted source (around line #5):

2: 3: <p> 4: <b>Name:</b> 5: <%= @status.user.full_name %> 6:
7: </p> 8: Rails.root: /Users/krishna/Treebook2

Application Trace | Framework Trace | Full Trace app/views/statuses/show.html.erb:5:in _app_views_statuses_show_html_erb___616901333193256108_2168842020' app/controllers/statuses_controller.rb:18:inshow' Request

Parameters:

{"id"=>"4"}

6 Answers

Stone Preston
Stone Preston
42,016 Points

did you add

belongs_to :user

to your status model and

has_many :statuses 

to your user model?

It looks like rails doesnt know that the @status variable has a user component to it, therefore its throwing the error that there is no method user. I could easily be wrong, since there isnt much context to your question, but try adding the above associations and see if that fixes it

Also those associations may be from a later video, but its worth a shot

Donal Phipps
Donal Phipps
16,389 Points

Thanks a lot - I was suffering with this problem and your answer really helped out. Must pay attention!

Krishna Ramachandran
PLUS
Krishna Ramachandran
Courses Plus Student 5,559 Points

Hey Stone thanks for your response. I had included the code you had mentioned above. I went back and watched the videos again to see if I missed something and redid it but still no luck.

I got this message as well when I changed a few things around:

 undefined local variable or method `status' for #<#<Class:0x00000103b50e60>:0x00000103990ff8>

Not sure what's wrong, or why either the "status" or "user" is showing up as undefined. I logged in with First, Last, email, user name, password and so I assume those were saved as variables under "User" (I checked using rails console and all of those variables are saved for my user account, but somehow it seems they are not being pulled when I try to call) Maybe it has something to do with that, but I don't know for sure and am not sure how to check for that. Help would be great!

Stone Preston
Stone Preston
42,016 Points

what view file are you rendering thats throwing the error

Krishna Ramachandran
PLUS
Krishna Ramachandran
Courses Plus Student 5,559 Points

I'm not 100% sure if this is right but it's show.html.erb and index.html.erb . Those are the two files that I am trying to add this line of code to: <%= @status.user.full_name %>

And not able to. I also tried:

 <%= @status.user.first_name %>

Which should have also worked I think, but no luck.

Krishna Ramachandran
PLUS
Krishna Ramachandran
Courses Plus Student 5,559 Points

I talked to someone and got a few insights into the issue.

I figured out that the statuses created are not being assigned to the user, which is why a nil value for user is appearing.

I believe somewhere in the controller I need to make statuses be assigned to the user. I thought by adding belongs_to :user in our models we made that connection, but it doesn't seem to be so. Any tips on how to make that connection in the controller?

Thanks

Stone Preston
Stone Preston
42,016 Points

hmm does your statuses database table have a user_id field. I like using this tool to view my database. If your table does not have the user_id field, you will have to generate a new migration and add the following code to the change method:

add_column :statuses, :user_id, :integer
add_index :statuses, :user_id

Note this code also adds the index. Check your db/migrate folder to see if you already have a migration that contains this code and if you do run rake db:migrate again.

Krishna Ramachandran
PLUS
Krishna Ramachandran
Courses Plus Student 5,559 Points

Thanks Stone for the input, I actually had the line of code you mentioned. I ended up finding a solution the problem. What I did was went to Controllers and statuses_controllers.rb file and added a line of code.

At the point in the page with the following code def create @status = Status.new(params[:status]) we added the following line of code after it. @status.user = current_user

This assigns the current user who is logged in to the status as it is being created by them. Which got around the problem that the user was not being properly assigned to the status.

Yehuda Silbermintz