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 Build a Simple Ruby on Rails Application Customizing Forms Adding a Dropdown

first_name not recognized as method!

I am using Rails 4 and have been struggling to get the user's name shown in the show.html when I follow what is said in the video it tells me that: "undefined method `first_name' for #<Status:0x2cba400>"

I have tried to get around this, but am getting seriously stuck.

Any help is appreciated.

5 Answers

Nathaniel, did you find a solution to this problem?

Nathaniel Lough
Nathaniel Lough
9,215 Points
  • Hey bud, I should mention that some of what I was saying about user_id's is explained if you keep watching the video.
  • Sorry this is so messy, I tried to format it but it just crunched it up. Is there some way to force spacing and line break?

Hey Griffin,

Well I guess I did. Here's what's up. I was up until about 4 last night trying to solve this. Three things contributed to me getting passed this problem.

1) I more or less copied someone's project I found on Github, file by file

2) My DB was messed up? I don't know how.

3) If you try to post a new status, but don't define a valid user_id in the form, your list of statuses will never display because it will keep trying to display a status that has a user_id that doesn't pertain to a user with a first and last name. So when you post a message, if you are typing 3...4...5... into the first box, it will break.

So you will need to clear out your statuses every time you screw up.

     rails console

     Status.delete_all

     exit

This was really the main problem I was having. The other guy's solution was cool. He provided a drop down menu that lists all known users and lets you select one. I'm currently trying to force the user to be the person who is signed in.

Here is how I "reset" my database to get a fresh one.

bundle exec rake db:reset

bundle exec rake db:migrate

Here is the guy's project that I referenced:

https://github.com/stshoot/treebook

To get it I just:

made an empty directory

went in it and did 'git init'

then 'git clone [his address from his github]'

went inside that folder

ran 'bundle'

ran 'rake db:migrate'

ran 'rails s'

Here is my project:

https://github.com/Captainlonate/treebook

A couple of things I noticed ( but perhaps didn't understand )...

1) My application_controller.rb needed some code added to it to work

2) status_params inside of statuses_controller.rb is where you add the "attr_accessible" stuff from the videos.

Mine looks like this:

 def status_params

       params.require(:status).permit(:content, :user_id)

 end

3) Some people on here recommended getting a different gem, it was called "protected " something... anyways I didn't find that necessary.

If none of that made sense to you... go get that guy's project and start comparing it to yours. Make sure that when you post a message, you signed in and used a valid user_id in the first input box. Also, Make your two controllers look like mine.

Let me know if I can help somehow.

Nathaniel,

Thank you for this post, it has helped me quite a bit. However it is still frustrating for me to compare my code to his and have the identical setup and have my database still not be functioning. It would be nice to have figured it out, but I should probably move on. Cheers, I will keep in touch!

Griffin

Nathaniel Lough
Nathaniel Lough
9,215 Points

Would you like to instant message with Google Chat?

Sure. How would we set that up?

sent!