Is it possible to use helpers in javascript assets in Ruby on Rails? I'm trying to convert Money object Object { fractional: "21050.0", currency: Object, bank: Object }
with avaiable helper humanized_money
. In normal view it's working properly <%= humanized_money_with_symbol my_money_object %>
but in js.coffee file it shows this error in javascript console.
ReferenceError: humanized_money is not defined
My .js.coffee file
$('.select2-hidden-accessible').change ->
item_id = $(this).find(":selected").val()
unit_price_input = $(this).parent().find('.unit_price')
unit_price = 0;
$.ajax "/items/#{item_id}.json",
type: 'GET'
dataType: 'json'
error: (jqXHR, textStatus, errorThrown) ->
console.log(textStatus)
success: (data, textStatus, jqXHR) ->
console.log(data['unit_price'])
unit_price = data['unit_price']
unit_price_input.val(humanized_money unit_price)
Solution
I've changed show.json.jbuilder to response with humanized_money instead doing this in view.
json.extract! @item, :id, :unit, :created_at, :updated_at
json.unit_price humanized_money @item.unit_price