如何在 Gon gem 中使用 Jbuilder 中的 Rails 助手?

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

我正在使用 Jbuilder 和 Gon 将我的 JSON 响应构造成变量并将它们传递到我的 JS 前端。在 Jbuilder 文件中,我想从 Money-Rails gem 访问我正在使用的辅助方法。但是,如果我尝试这样的事情...

// edit.json.jbuilder

json.minimum_premium money_without_cents limit.minimum_premium

我收到 undefined method `money_without_cents' for #<Gon::Jbuilder::Parser:0x00007fbb6ebca198> 错误。

在使用 Gon gem 时,在 Jbuilder 中使用辅助方法是否有技巧?

这有效,尽管可能有更好的解决方案...

// edit.json.jbuilder

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