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