● videoThe Rewrite
Filtering Routes

Unlock Revisiting Ruby on Rails
Subscribe for full access to every course, or buy this one on its own.
SECTION
The Rewrite
NEXT UP
Some Tips and Tools
COURSE
Rails Revisited
33 lessons
About this lesson
Filters are used to run methods before or after a controller action is fired. Sometimes both, using around. The scaffold uses the before filter to set the resource instance for the routes that need it, and you can also use filters to ensure that users are authorized for a given route.
A good place to put this logic is in the base controller, our application_controller:
<span class="hljs-keyword">class</span> <span class="hljs-title class_">ApplicationController</span> < <span class="hljs-title class_ inherited__">ActionController::Base</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">is_admin!</span>
redirect_to root_path <span class="hljs-keyword">unless</span> user_signed_in? && current_user.is_admin?
<span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
Make sure there's a redirect on this logic! It's tempting to return true or false, but that won't work for our needs.
Unlock Revisiting Ruby on Rails
Subscribe for full access to every course, or buy this one on its own.