Welcome to the Treehouse Community
Looking to learn something new?
Undefined method for first_name after creating relationship in status.rb and adding first_name to show.html.erb
I get an undefined method error for "first_name" when attempting to add a new status or modify a status.
Looking at my migration files, my first table is "users" and first_name, last_name, and profile_name are added. When I'm attempting to create an input field for first name in show.html.erb I added the erb tag <%= @status.users.first_name %>
What could I be doing wrong?
- My database is migrated
- The tags seem correct
- When I create a status, I get the undefined method error but if I refresh my statuses page, the status I attempted to create is there.
- When I try to edit a status and update, I get the same undefined method error again
- I'm using rails 4.0.0 and ruby 2.0.0p247 on Mac OSX Mavericks
- I've restarted my server
- Cleared my previous statuses via the rails console
- It might be useful to note that attr_accessible isn't in rails 4.0 so I don't have that field in any .rb files
Also, I apologize for the formatting. It looks like markdown code isn't working here.
Mike Morales19,833 Points
Wow! You've made it this far using ruby 2.0.0! Anyways, I ran into the same problem as you did. Delete all of your statuses if you haven't already done so. Type in rails console at the prompt, hit enter, type in u=User.first, enter. Your correct user is shown in the list of information. Use that User id to create a new statuses. See if that fixes your problem? It worked for me, though. Good luck!
Hey Mike, I didn't have any luck. It seems I'm experiencing a few issues. When I create user accounts, all of the parameters aside from email and password contain nil records. I managed to fix that using the following https://teamtreehouse.com/forum/problem-with-devise-in-simple-ruby-on-rails-app
Now, when I create user accounts, all of the information I input gets stored.
So I deleted all statuses, deleted all user accounts and started from scratch.
I can get to the statuses page, when I create a status and do not input an "ID" I get the same undefined method error for "first_name".
So I erased everything again, except for the user account which had an ID of 7 and went back to the statuses page to create a status. When I created the status and input the ID as 7, undefined method error for "first_name" again.
in statuses_controller.rb you have to permit user_id to be input by adding :user_id to line 72 like this
def status_params params.require(:status).permit(:name, :content, :user_id) end
Mike Morales19,833 Points
Awesome! Most of the issues, I've encountered were easily solved by correcting any small errors, I made on my part, or just by restarting the browser. But I know it's not always that simple to figure out.
I feel your pain. As I progress, I'm beginning to understand my errors more and more. I think half of the fight is just understanding the file structure and how everything works together.