Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
View Problems - Incorrect Name1:58 with Jay McGavren
What if you've added a view, and it's not being found?
What if you've added a view and it's not being found. 0:00 When we visit the Edit form for a pet and update a pet to have a blank name 0:03 we should be redirected back to the form to fix the name but 0:09 instead we get a Template is missing error. 0:12 If we look in the app/views/pets directory. 0:15 We'll see an edit.html.erb. 0:23 So what's the problem? 0:26 Well let's look at the error in the browser again. 0:27 It says Missing template pets/edits. 0:30 Note the s on the end. 0:33 There's just one method in the stack trace, 0:35 the update method on pets_controller line 38. 0:37 So let's open app/controllers/pets_controller and 0:42 go to line 38. 0:46 And there we see the code render :edits where the view name, edits, is plural. 0:46 Let's look at the components of the error message to determine what template 0:52 filename Rails is looking for. 0:56 We can see that it's looking in the app/views folder. 0:58 And because our call to render is in the pets controller 1:01 it's looking in the pets sub folder. 1:04 But because we're calling render edits, plural, it's looking for 1:06 a template named edits.html.erb. 1:10 Our actual template file is named edit.html.erb. 1:14 So let's change our controller code from render :edits to render :edit, 1:19 save, and reload the page. 1:24 If we're asked to confirm the form resubmission, we can. 1:27 We should get redirected back to the Edit page to fix the pet name and 1:30 the template loads. 1:34 If we go look at the rails log, we see that it rendered pets/edit.html.erb 1:35 instead of pets/edits.html.erb, the plural. 1:40 Fixing our code allowed it to load the views template file. 1:45 Now, you have some general tips on how to troubleshoot problems with your views. 1:48 Next, we'll look at how to fix issues you can encounter while loading 1:53 model data from your database. 1:56
You need to sign up for Treehouse in order to download course files.Sign up