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

current_user creates post

I'm using devise for user authentication and I'm having the user create statuses. Unlike the Rails tutorial, I do not want to have the user pick from a list of users to post as. I just want it to make something like (post.user_id = current_user) on the Post Controller 'create' method but that's not working.

Here's what I have in my post controller:

def create @post = Post.new(params[:resume]) @post.user_id = current_user end

Can anyone tell me what I'm doing wrong?

S/N: When listing out the resumes I'm using:

def index @resumes = Resume.where(:user_id => current_user) end

^^ and that's working. Figured it would work on create as well. I appreciate any help. It's probably something stupid I'm forgetting.

1 Answer

Brandon Barrette
Brandon Barrette
20,485 Points

That's because when you call current_user, you are calling the whole row of the database and not just the id. So you need to have

@post.user_id = current_user.id

Also, if you've set up and association between users and resumes, meaning:

user.rb  has_many :resumes
resume.rb    belongs_to :user

Then you could just use:

@resumes = current_user.resumes

Hope that makes sense.