0

Im trying to use Money-Rails with my Rails 4 app.

I have a participants model which has attributes as follows:

t.boolean  "costs"
t.integer  "participation_cost_pennies",  default: 0,     null: false
t.string   "participation_cost_currency", default: "GBP", null: false

Users can select their currency and that should override the default GBP setting.

Im my view, I have:

<% if @project.scope.try(:participant).try(:costs) == true %>
           <%=  @project.scope.try(:participant).try(humanized_money_with_symbol :@participation_cost_pennies) %>

When I try the above, the view shows up blank (no display of anything). When I try to inspect the element in the google browser, I can only see an empty span tag in which that code is set out (with nothing).

I previously tried the following, bit it displayed the price in pennies (as in $6 as 600 USD:

      <%=  "#{@project.scope.try(:participant).try(:participation_cost_pennies)} #{@project.scope.try(:participant).try(:participation_cost_currency)}" %>

Can anyone see what I've done wrong? I'd like to display $4 instead of 400 cents/pennies.

Thank you

Mel
  • 2,481
  • 26
  • 113
  • 273

1 Answers1

1

humanized_money_with_symbol is a helper, so you use it in the form humanized_money_with_symbol @money_object.

In your case that means you would use it like so:

<%=  humanized_money_with_symbol @project.scope.try(:participant).try(:participation_cost_pennies) %>

all try does is call a method on the object you've chained and return nil if it doesn't exist. What you were doing was call humanized_money_with_symbol on a method named :@participation_cost_pennies on participant, which didn't exist, so try ended up returning nil

Mario
  • 1,349
  • 11
  • 16
  • Hi Mario. thanks for the tip - I just tried that and it still doesn't work. Still renders blank on the view. – Mel May 10 '15 at 02:50
  • @user2860931 yeah sorry about that, I just noticed I left an '@' that shouldn't be there (you had it in your code). Check the edit – Mario May 10 '15 at 02:51