That's not broken... Re-think your design

November 24, 2016

It has been months since I have posted on this blog and I have learned a TON in that amount of time. I'm restarting this blog to share some a few things I learn each month. To start off, here's some Devise.

We have all used the very popular Rails engine ‘Devise.’ A couple weeks ago I came across a new use case with the gem about overriding one of their standard methods. I was dealing with the mailer and a client wanted to redirect to a new page after a user confirmed their account via email. Simple enough. The problem was my agency uses a custom rails engine that preinstalls Devise and therefore some of it's initial configuration; like overriding the 'after_confirmation_path' method.

First I tried overriding Devise's method just like their docs suggest, but that didn't work for some reason. Maybe I source the gem from my local (instead of and update its child method 'after_confirmation_path' to fit my needs? That could work if I was doing some serious customization to the overall gem, but I clearly was not. I also tried setting the Devise mailer to use my own mailer in its configuration. No luck.

Then I considered calling the 'grandparent' (which would be the devise method) and overriding it for the project I was in. Apparently this is a thing, but there was no way that it felt right for doing something so simple. This was my first hint that I was on the wrong track. (stack overflow dealing with grandparent override). I also tried setting our custom gem's mailer to use my own mailer in its configuration. No luck.

In the end, the solution was a simple configuration that I had overlooked. I'm currently learning Ember.js and I stumbled upon this quote the other day that relates to my problem here: "It sounds like right now you're working hard to do something that Ember deliberately discourages. Generally I find when I'm fighting the framework it's because my understanding of the problem is different (and often not as complete as the framework authors)."

Devise.setup do |config|
config.mailer = 'UsersMailer'

The lesson here: Pay attention to signs that you are on the wrong path and moving too quickly. When solving simple problems look for simple solutions. Don't reinvent the wheel. I should have spent more time looking at my first solution instead of dismissing it and thinking there was something wrong with my apps design, or Devise.

Here's the end product, which Devise easily points to in their docs
class Users::ConfirmationsController < Devise::ConfirmationsController


def after_confirmation_path_for( resource_name, resource )
super + "?email=#{params[:email]}"



comments powered by Disqus