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

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
STAFF
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])