How can I use a Rails helper inside Jbuilder with the Gon gem?

1.6k Views Asked by At

I'm using Jbuilder and Gon to structure my JSON responses into variables and pass them to my JS front-end. In the Jbuilder files, I'd like to access the helper methods I'm using from the Money-Rails gem. However, if I try something like this...

// edit.json.jbuilder

json.minimum_premium money_without_cents limit.minimum_premium

I get an undefined method `money_without_cents' for #<Gon::Jbuilder::Parser:0x00007fbb6ebca198> error.

Is there a trick to using helper methods inside Jbuilder when using the Gon gem?

1

There are 1 best solutions below

0
On BEST ANSWER

This worked, though there might be better solutions...

// edit.json.jbuilder

json.minimum_premium ActionController::Base.helpers.money_without_cents limit.minimum_premium