【问题标题】:Rails string expressionRails 字符串表达式
【发布时间】:2015-05-01 06:24:49
【问题描述】:

我正在使用 Rails 4 并尝试在我的显示页面中显示货币和金额。

我有一个具有费用、货币和参与成本属性的模型。费用是一个布尔值。如果为真,则应显示货币和费用。

如果项目成本有参与费用,我想显示金额后跟货币,如下:

 <% 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" %>

但是,这个输出只是参与者成本的数字,没有货币指标。数据库存储了一种货币。

项目模型有:

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

谁能看到我做错了什么?

谢谢

【问题讨论】:

  • 发布您的Project 模型。
  • 您似乎在 #{@project.scope.try(:participant).try(:currency) 中调用了 participant.currency 的等价物。每个参与者是否跟踪自己的currency
  • 嗨 Fylooi。我的参与者模型中有货币和金额费用的属性。用户选择币种和参与成本。我试图创建一个字符串来在视图中显示货币和金额。

标签: ruby-on-rails


【解决方案1】:

Rails 提供了将数字转换为货币格式的助手。默认情况下,它假定货币为美元,但您可以通过传递参数来自定义它。

 number_to_currency(number)

更多详情您可以从http://apidock.com/rails/ActionView/Helpers/NumberHelper/number_to_currency获得

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-18
    • 2019-11-09
    • 1970-01-01
    相关资源
    最近更新 更多