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
Kayci Barnett
5,349 PointsCreating Relationships
I'm stuck on creating a relationship. I've tried twice now. What I noticed is that my program doesn't keep the 1 when I assign it to the post. If I edit status and assign 1, it says I successfully edited the status. When I click show, the 1 is gone. If I hit the back button the 1 is there for a second, and then disappears. When I try to move forward on the project, and add the code to the show portion I get this error message. Showing /Users/kaycibarnett/treebook/app/views/statuses/show.html.erb where line #5 raised:
undefined method `first_name' for nil:NilClass Extracted source (around line #5): 2 3 4 5 6 7 8
<p> <strong>Name:</strong> <%= @status.user.first_name %> </p>
<p>
14 Answers
Brandon Barrette
20,485 PointsSo it sounds like the user is not being saved to the status. I recommend deleting all statuses, and trying again.
In your terminal, type:
rails console
This gets you to the database to do some work with it. Then type:
Status.delete_all
This will delete all statuses. Then go back to Treebook, and create a new status. If you get the same error, then I need to see the code in your controller, because then the status is not being assigned a user.
Kayci Barnett
5,349 PointsHi Brandon, thanks so much for trying to help me figure this out. I got a new error message after I followed your instructions, maybe that is a good sign?
ndefined method `statuses' for nil:NilClass
Extracted source (around line #27): 25 26 27 28 29 30
# POST /statuses.json def create @status = @user.statuses.new(status_params)
respond_to do |format|
if @status.save
Brandon Barrette
20,485 PointsWhere do you define @user? That's why it's throwing an error right now.
Kayci Barnett
5,349 PointsI'm not sure where we defined the user, but I looked in my code under the app, and models folder is a user.rb file and here is the code for that. Is that what you meant? I also have the code uploaded to git hub if that is helpful.
class User < ActiveRecord::Base # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable attr_accessible :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :profile_name
def full_name
first_name + " " + last_name
end end
Brandon Barrette
20,485 PointsWhat's your github link?
Kayci Barnett
5,349 PointsBrandon Barrette
20,485 PointsOk, i found your error. Took me awhile because you're on Rails 4 and these videos are written for Rails 3.2
This line in your statuses controller
def status_params
params.require(:status).permit(:name, :content)
end
Says that the params for statuses should only permit name and content. However, you want it to permit the user_id since you want the user to save to the status, so it should read:
def status_params
params.require(:status).permit(:user_id, :content)
end
When you removed the :name from the database, you forgot to add in the :user_id. Remember this throughout your work to save a lot of headaches.
Kayci Barnett
5,349 PointsBrandon, again thank you so much for your help. However, my troubles go deep! Here is the latest error message. .. NoMethodError in StatusesController#create undefined method `statuses' for nil:NilClass
Extracted source (around line #27): 25 26 27 28 29 30
# POST /statuses.json def create @status = @user.statuses.new(status_params)
respond_to do |format|
if @status.save
Brandon Barrette
20,485 PointsWell, I got statuses to be created once I changed that code. Make sure you are making a status with a user_id that exits in your database. In rails console, you can type
User.all
to list all the users. From there, you can make sure you pick the right user_id.
Kayci Barnett
5,349 PointsWhen I did that it looked like my only user was 6, so I assigned number 6 to the post, and got the same error message :( What is weird is that it shows my first, last, and profile name as nil, but when I signed up I provided all that. here is what console said
User Load (36.8ms) SELECT "users".* FROM "users" => #<ActiveRecord::Relation [#<User id: 6, first_name: nil, last_name: nil, profile_name: nil, email: "kuhpow@gmail.com", encrypted_password: "$2a$10$nwHodcC/aUohO0XP1cw7YetJZkJmBm/L9jg28Aw7kNr....", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2013-11-16 19:46:26", last_sign_in_at: "2013-11-16 19:46:26", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-11-16 19:46:26", updated_at: "2013-11-16 19:46:26">]>
Brandon Barrette
20,485 PointsOk, from the console, try this:
U = User.find(6)
U.update_attributes(first_name: "put it here", last_name: "same", profile_name: "here")
U
That should help you update those fields. Then delete all statuses from the database in your console:
Status.delete_all
Then try making a new status again with id = 6.
Kayci Barnett
5,349 PointsOkay I tried that, and got this error message: NoMethodError in StatusesController#create undefined method `statuses' for nil:NilClass
Brandon Barrette
20,485 PointsThen I'm not sure, I'm out of ideas. You can try pushing through, eventually in the create action you remove the user_id input in the form and just assign the current_user to the user_id in the status.
Kayci Barnett
5,349 Pointswell thanks anyway for your help, I appreciate it
Gabriel Morales
11,315 PointsThis worked for me! Thanks Brandon! I had been stuck on it for some time now!