1
votes

I'm using Rails 4 and trying to display a currency and amount in my show page.

I have a model which has expenses, currency and participation_cost attributes. Expenses is a boolean. If true, then the currency and the expenses should be displayed.

If a project cost has participation expenses, I'd like to display the amount followed by the currency, as follows:

 <% if @project.scope.try(:participant).try(:expenses) == true %>
          <%=  "#{@project.scope.try(:participant).try(:participation_cost)} #{@project.scope.try(:participant).try(:currency)}" %>
         <% else %>
          <%= render :text => "Participants do not receive participation costs or payment" %>

However, the output of this is only the number for participant costs, without the currency indicator. The database has a currency stored.

Project model has:

class Project < ActiveRecord::Base

  include RoleModel
  # --------------- associations


  #belongs_to :students, through: :courses, counter_cache: true

  has_many :project_questions#, through: :projects
  has_many :project_answers#, through: :project_questions

  has_one :scope
  accepts_nested_attributes_for :scope
  has_one :educator_project_comment
  has_many :project_student_eois
  belongs_to :educator
  has_many :project_invitations

  has_many :observations

  has_one :approval
  belongs_to :industry

  has_and_belongs_to_many :users


  mount_uploader :hero_image, AvatarUploader
  mount_uploader :link_to_video_proposal, VideoUploader
  # --------------- scopes

Can anyone see what I've done wrong?

Thank you

1
Post your Project model.potashin
You seem to be calling the equivalent of participant.currency in #{@project.scope.try(:participant).try(:currency). Does each participant keep track of its own currency?fylooi
Hi Fylooi. I have attributes for both currency and amount_expenses in my participant model. Users select the currency and the cost of participation. I was trying to make a string to display the currency and the amount in the view.Mel

1 Answers

0
votes

Rails provide helper to convert numbers to currency format. By default it assume currency is USD but you can customize it by passing parameters.

 number_to_currency(number)

More details you can get from http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency