I love my church! (Taken with instagram)
Beautiful memorial day! (Taken with Instagram at Coney Island)
God is so for you as your defender that he is against himself as your accuser.
Human buffering… (Taken with instagram)
Chloe is the coolest dog. (Taken with instagram)
$400 Water bottles. (Taken with Instagram at The Box Jelly)
This guy inspires me to never give up! (Taken with instagram)
I’m in the process of converting an existing Rails app into an Engine. It’s turned out to be a trying task. One thing that stumped me today was properly implementing CanCan in the engine’s newly namespaced environment. My controllers that implement CanCan’s load_and_authorize_resource method were namespaced in an Admin module.
Initially, I continued to receive this pretty useless error:
Apparently, CanCan builds its reference to the resource model object by singularizing the controller’s namespace and referencing it as a constant. Instead of a reference to my MyEngine::User model, CanCan references by default MyEngine::Admin::User. To fix this, CanCan provides a :class option to predefine the resource class name (more about this here). I simply modified my method call to this:
Unfortunately, this was only half the problem. Although CanCan now knew about the correct resource model, it did not know where to find the correct Ability class:
CanCan has a method called current_ability that “creates and returns the current users’s ability and caches it.” The problem is, the Ability that the method instantiates is not correctly referencing the namespaced Ability class. My Ability class lives in app/models/my_engine/ability.rb and I needed to notify CanCan that it exists there. I did this by re-defining the current_ability method in my Admin::ApplicationController:
That was a journey of dependency hell I hope no one else has to go through. I hope this helps some other thirsty traveler.
Big in Japan. (Taken with instagram)
Page 1 of 173