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!

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


Passing user_id without using simple form input tag?

So I completed simple RoR application tutorials. Before I continue with implementing friends I want to become more confident in RoR. Currently, the tutorials left us choosing the user we want to post as. I don't want to do that. I'd rather have the form just now who you are. This is what I did:

<% if user_signed_in? @status.user_id = current_user.id end %>

and to validate that everything was set correctly I added

<%= @status.user.full_name %>

This works but only in the scope of the form. Once I try to add the new status the index page throws an error stating the user is nil.

How do I pass the current_user to the new status object?

Best, Ramsey Khadder

1 Answer

Jason Seifer
Jason Seifer
Treehouse Guest Teacher

Hey Ramsey Khadder we do this a bit later in the lessons but you'd want to do this in the controller in the create method:

@status = Status.new(params[:status])

Change that to:

@status = current_user.statuses.new(params[:status])