Search code examples
ruby-on-railshelper

Rails: Unexpected tSTRING_BEG in rails helpers


I am trying to create a helper function to be used in my view for my Rails application.

I am using the number_to_human ActionView::Helpers::NumberHelper method for formatting the values.

This is my implementation of the function:

def money(value, currency)
  return "N/A" unless value
  number_to_human(value, units: { thousand: 'K', million: 'M' }) "#{currency}"
end

What I want to achieve is to parse the value of currency, which can be $ OR any other currency depending on what I want.

However, I get the error below when I try testing:

app/helpers/formatting_helpers.rb:18: syntax error, unexpected tSTRING_BEG, expecting keyword_end ...housand: 'K', million: 'M' }) "#{currency}" ... ^

If I remove the "#{currency}", it works fine. But I need a way to pass the value of currency into the function.

How do I go about fixing this please.


Solution

  • You should parse the return value correctly, e.g.:

    def money(value, currency)
      return "N/A" unless value
      number = number_to_human(value, units: { thousand: 'K', million: 'M' }) 
    
      "#{number} #{currency}"
    end