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