Only access the request object when possible

Resolves errors when `@controller` is `nil`, e.g. rake tasks and console. This change follows the example of the checks in `self.controller` and `self.action`.