Using dry monads mixins in Rails views


I was wondering if it’s possible to just use Failure() in a Rails view and if so how to mix those modules in. Simply doing include Dry::Monads[:result] in the controller works for the code inside controller actions, but doesn’t seem to cascade in any way to the views. I’m getting a

NameError - uninitialized constant #<Class:0x00007f46c356cac8>::Failure:

failure if I do that.

I create a shortcut constant for these situations. T is my Types shortcut, M is my Dry::Monads shortcut. You could just reference M::Failure() as you would with the mixin method.

Side note: as I mentioned elsewhere, I don’t think using Monads in view logic is a good idea for the same reasons why using branching logic is a bad idea.

1 Like