1 00:00:00,670 --> 00:00:04,370 In our pages controller, several action methods have code that takes the ID 2 00:00:04,370 --> 00:00:10,780 parameter, finds a page object with that ID and stores it in an instance variable. 3 00:00:10,780 --> 00:00:15,000 We have code to do this exact same operation at the top of the show, edit, 4 00:00:15,000 --> 00:00:17,430 update and destroy methods. 5 00:00:17,430 --> 00:00:19,840 Again, the don't repeat yourself principle applies. 6 00:00:19,840 --> 00:00:22,930 It's usually better to avoid repeated code. 7 00:00:22,930 --> 00:00:25,860 So let's do like we did with the page params method and 8 00:00:25,860 --> 00:00:28,310 move the repeated code to a separate method. 9 00:00:28,310 --> 00:00:32,900 We'll copy the page = page.find(params id) code 10 00:00:32,900 --> 00:00:37,710 out of the show method and move it to a set page method. 11 00:00:37,710 --> 00:00:41,387 We'll put this under the private keyword since this method should only 12 00:00:41,387 --> 00:00:43,724 be accessed within the page's controller. 13 00:00:49,243 --> 00:00:50,260 Paste start code there. 14 00:00:52,510 --> 00:00:55,964 Then we'll go back through the show, edit, update, and 15 00:00:55,964 --> 00:01:00,133 destroy methods and replace the repeated code with a call to set page. 16 00:01:07,650 --> 00:01:09,440 Now that's one way to do it. 17 00:01:09,440 --> 00:01:12,610 But running a setup method at the start of a controller action is so 18 00:01:12,610 --> 00:01:15,720 common that Rails provides another shortcut. 19 00:01:15,720 --> 00:01:19,846 At the top of your controller class outside of any instance methods, 20 00:01:19,846 --> 00:01:22,206 you can call the before action method. 21 00:01:24,526 --> 00:01:30,060 You can pass before action a symbol with the name of a method, set page. 22 00:01:30,060 --> 00:01:30,955 With this set up, 23 00:01:30,955 --> 00:01:35,580 the controller will call the set page method before running any action method. 24 00:01:35,580 --> 00:01:38,530 You can remove all of the explicit calls to set page. 25 00:01:44,270 --> 00:01:46,070 Let's save this and go to our browser and 26 00:01:46,070 --> 00:01:49,070 see what happens if we try to reload the list of all pages. 27 00:01:50,070 --> 00:01:51,680 We get a record not found error. 28 00:01:51,680 --> 00:01:54,430 Couldn't load page with an ID of blank. 29 00:01:54,430 --> 00:01:56,860 This happens because of course the index new and 30 00:01:56,860 --> 00:01:59,900 create methods don't have an ID parameter. 31 00:01:59,900 --> 00:02:02,260 There's none in the URL for it to load. 32 00:02:02,260 --> 00:02:06,690 But Rails will try to run the set page method anyway resulting in an error. 33 00:02:06,690 --> 00:02:11,220 So you may want to add the except argument on to your call to before action. 34 00:02:13,150 --> 00:02:16,719 Except takes an array of symbols with the names of methods. 35 00:02:16,719 --> 00:02:24,021 So we'll give it method index, new and create. 36 00:02:24,021 --> 00:02:26,639 Rails won't run the setup method before 37 00:02:26,639 --> 00:02:29,489 any action that appears in the except list. 38 00:02:29,489 --> 00:02:34,439 If we reload our browser, we'll see that the index, 39 00:02:34,439 --> 00:02:39,068 new and create methods all work correctly again. 40 00:02:39,068 --> 00:02:43,470 This is because Rails isn't calling set page before them anymore. 41 00:02:43,470 --> 00:02:50,850 It'll still run set page before the show, edit, update and destroy actions, though. 42 00:02:50,850 --> 00:02:54,980 If we maintain a list of the actions we don't want to run with set page, though, 43 00:02:54,980 --> 00:02:59,170 we have to remember to add to the list any time we add another action method. 44 00:02:59,170 --> 00:03:01,870 We might be better off maintaining a list of the actions we 45 00:03:01,870 --> 00:03:03,760 do want to run with set page. 46 00:03:03,760 --> 00:03:09,820 We can do that by changing the except argument to only and 47 00:03:09,820 --> 00:03:15,620 changing the list of method names from index, show and create to show, 48 00:03:15,620 --> 00:03:21,127 edit, update and 49 00:03:21,127 --> 00:03:24,090 destroy. 50 00:03:24,090 --> 00:03:27,630 Now the before action will run before only these methods. 51 00:03:28,640 --> 00:03:30,647 If we try all the actions out in our browser, 52 00:03:30,647 --> 00:03:33,046 we'll see that everything still works correctly.