Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Adding Job Owners to Views4:35 with Jim Hoskins
[?music?] 0:00 [Master Class: Designer and Developer Workflow: Adding Job Owners to Views] 0:02 [Jim Hoskins] So now we've added the user_id field to the jobs table 0:05 and associated jobs to users and users to jobs. 0:08 $rails c 0:12 And in the last video, we even associated a job with a user-- 0:16 for instance, User.first.jobs, 0:20 so there is one job in the database that has a user associated with it. 0:26 So let's look at what we can do with that. 0:30 Let's go into our apps/views folder/job 0:32 and the show.html.haml, which is the page that displays when we actually click on a job. 0:34 So what we might want to do is below company name, 0:44 create another div called .user 0:48 and here we'll just put @job.user and let's see what happens there. 0:52 So now we see we get a pretty poor representation of our user, 1:00 but we can see that there is one associated with it 1:04 and that's because the Ice Cream Tester job does have a user associated with it. 1:08 If we go to the Fashion Police Officer, nothing shows up in that space, 1:11 so what do we want to do? 1:18 Well, we could add user.name, which might be what we want to show, 1:20 and if we were to go back to the Ice Cream Tester, 1:25 we could see that Jim Hoskins is posted here, 1:30 but if we go to Fashion Police Officer, we're going to get an error 1:33 and that's because the jobs user field is blank or nil, 1:37 and when we try to call name on that nil response, 1:42 we're going to get a nil method error. 1:46 Now, there are a couple of different ways we could handle this. 1:49 Now, in our particular case, we're going to end up having it so that no job can exist 1:51 without a user associated with it, and that's really a validation issue, 1:57 but right now, our data doesn't fit that. 2:02 But other situations where you might use this, 2:04 having the ability to have a nil value in the association might be what you want, 2:07 so one strategy we could use instead of calling name on the user, 2:11 we could do something like override the to_s method on user. 2:14 Instead of getting an ugly representation like this, 2:20 we can actually control what it prints out. 2:23 So we'll go to the user.rb, define to_s 2:26 and all we need to do is return name, for instance, 2:32 and we'll save that out and if we refresh, we can see that it's now Jim Hoskins. 2:35 But if we go back to a different one that doesn't have a user associated with it, 2:42 since the to_s for nil is nothing, we don't see anything. 2:46 Now, that's one solution. 2:51 Otherwise, we could do something in our view; for instance, use an if statement 2:53 to test whether or not a user is associated with this job. 2:57 So if we go into show, what we might want to do here is if there's not a job, 3:01 you may not want to show that user div at all, 3:06 so let's say if @job.user and we will indent everything beneath it 3:09 and this code will only be added to our markup if job.user is true. 3:17 So what we could do is, say, add some literal text here: 3:22 Posted by 3:28 and we can see on this one, it's still empty, but if we go to this one, 3:30 we can see now we get Posted by Jim Hoskins. 3:34 And the added benefit of doing this if @job.user test 3:38 is that we could do something like @job.user.name, 3:43 which will return the same thing, but since we're now assured that job.user exists, 3:48 we can now safely call name on it and we can easily do things like link_to, 3:54 which will link to a specific model. 4:00 So we want the link's text to be our name and the link should point to job.user, 4:03 and Rails will figure out what the URL for the user is. 4:11 So if we click on this, it will go to user/1 if we wanted to link it up like that. 4:14 So overall, I think the most effective solution for us is to check if @job.user, 4:19 but later on, if we make it so that job.user has to be true, 4:25 we can get rid of the if statements 4:28 since hopefully, the validations will make sure that job.user is never nil. 4:30
You need to sign up for Treehouse in order to download course files.Sign up