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
At the top of your controller class, outside of any instance methods, you can call the before_action method, and pass it a symbol with the name of a method. With this set up, the controller will call that method before running any action method.
-
0:00
In our pages controller, several action methods have code that takes the ID
-
0:04
parameter, finds a page object with that ID and stores it in an instance variable.
-
0:10
We have code to do this exact same operation at the top of the show, edit,
-
0:15
update and destroy methods.
-
0:17
Again, the don't repeat yourself principle applies.
-
0:19
It's usually better to avoid repeated code.
-
0:22
So let's do like we did with the page params method and
-
0:25
move the repeated code to a separate method.
-
0:28
We'll copy the page = page.find(params id) code
-
0:32
out of the show method and move it to a set page method.
-
0:37
We'll put this under the private keyword since this method should only
-
0:41
be accessed within the page's controller.
-
0:49
Paste start code there.
-
0:52
Then we'll go back through the show, edit, update, and
-
0:55
destroy methods and replace the repeated code with a call to set page.
-
1:07
Now that's one way to do it.
-
1:09
But running a setup method at the start of a controller action is so
-
1:12
common that Rails provides another shortcut.
-
1:15
At the top of your controller class outside of any instance methods,
-
1:19
you can call the before action method.
-
1:24
You can pass before action a symbol with the name of a method, set page.
-
1:30
With this set up,
-
1:30
the controller will call the set page method before running any action method.
-
1:35
You can remove all of the explicit calls to set page.
-
1:44
Let's save this and go to our browser and
-
1:46
see what happens if we try to reload the list of all pages.
-
1:50
We get a record not found error.
-
1:51
Couldn't load page with an ID of blank.
-
1:54
This happens because of course the index new and
-
1:56
create methods don't have an ID parameter.
-
1:59
There's none in the URL for it to load.
-
2:02
But Rails will try to run the set page method anyway resulting in an error.
-
2:06
So you may want to add the except argument on to your call to before action.
-
2:13
Except takes an array of symbols with the names of methods.
-
2:16
So we'll give it method index, new and create.
-
2:24
Rails won't run the setup method before
-
2:26
any action that appears in the except list.
-
2:29
If we reload our browser, we'll see that the index,
-
2:34
new and create methods all work correctly again.
-
2:39
This is because Rails isn't calling set page before them anymore.
-
2:43
It'll still run set page before the show, edit, update and destroy actions, though.
-
2:50
If we maintain a list of the actions we don't want to run with set page, though,
-
2:54
we have to remember to add to the list any time we add another action method.
-
2:59
We might be better off maintaining a list of the actions we
-
3:01
do want to run with set page.
-
3:03
We can do that by changing the except argument to only and
-
3:09
changing the list of method names from index, show and create to show,
-
3:15
edit, update and
-
3:21
destroy.
-
3:24
Now the before action will run before only these methods.
-
3:28
If we try all the actions out in our browser,
-
3:30
we'll see that everything still works correctly.
You need to sign up for Treehouse in order to download course files.
Sign up