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

Ross Litzenberger
Ross Litzenberger
11,706 Points

Rails 4 working with devise need to have user connent to its jobs

So i am stuck right now.... I am using devise. i want to have to user that posted the job display the first_name and last_name but all i am getting in the user_id. here the github account https://github.com/RossLitzenberger/Jobboard

I'm not seeing how you are actually saving the user info to the database. Can you please let me know how you are doing that? Or is that you are manually inserting the records into your database?

Also what happened when you getting the user first and last name like so?

@job.user.first_name @job.user.last_name

3 Answers

Ross Litzenberger
Ross Litzenberger
11,706 Points

I just pulled down my application again

  1. rake db:create
  2. rake db:migrate 3 .When to app/views/jobs/show.html.haml . add @job.user.first_name @job.user.last_name

%p#notice= notice .job_show .job_header

    .show_job_title
        = @job.title
    .company_name
        = @job.company_name
    -# here the question i want to display the user that goes with the job that get posted.
    .user
        -if user_signed_in?
            Posted by
            = @job.user.first_name 
            = @job.user.last_name
.job_date
    POSTED ON
    %br
    = @job.created_at

%p.job_description
    = @job.description
= link_to 'Show More', @job.details_link, :class => "details_link"

.job_links
    = link_to 'Edit', edit_job_path(@job)
    |
    = link_to 'Back', jobs_path
  1. run rails s
  2. open the browers
  3. click register (build a user filled out everything)

Stop! open rails console and type User.first I saw that first_name last_name profile_name is "ni "but everything else was entered in

  1. Create a Job post
  2. I submit it
  3. the Posted by is not showing the first_name last_name profile_name

why are those field nil . when i fill all that out in register?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

Did you include first_name and last_name as permitted attributes for mass assignment? If not, they are not saved in the database and are therefore nil.

Ross Litzenberger
Ross Litzenberger
11,706 Points

How do i not allow other user to edit other users posts?

Maciej Czuchnowski
Maciej Czuchnowski
36,441 Points

If you're using Devise, you should check if the user that clicked the edit link (current_user) is the same user as the one who created it (post.user). If he's not, then redirect and flash an error.