Money-rails gem - 如何覆盖默认货币并显示小数点

时间:2016-04-28 11:51:15

标签: ruby-on-rails ruby rubygems money-rails

我正在使用rails-money gem首次为我正在构建的活动应用程序。我有一个事件模型,其中有一个类型为整数的'price'列。我希望用户能够输入他们想要的任何变化(在合理范围内)以获得事件的成本 - 例如£15.99,15.99等,但是节目输出需要看起来整洁和合适(15.99英镑)。目前我的输入字段允许我在表单上放置一个小数点,但它在显示页面上无法识别这一点(£15.99只显示为15)。此外,我有一个货币选择字段,其中3种主要货币作为选择£/€/ $但是无论我在显示页面上做出什么选择它都是$,因为上面的例子£15.99显示为15美元。我该如何解决这个问题?

这是我目前的代码 -

Event.rb

var scene = new THREE.Scene();
var sceneCSS = new THREE.Scene();
function render(dt) {
      effect.render(scene, camera);
      effectHUD.render( sceneCSS, camera );
    }

money.rb

 class Event < ActiveRecord::Base

belongs_to :category
belongs_to :user
has_many :bookings


has_attached_file :image, styles: { medium: "300x300>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/


monetize :price, with_model_currency: :currency 



end

_form.html.erb

    # encoding : utf-8

MoneyRails.configure do |config|

  # To set the default currency
  #
  #config.default_currency = :gbp

  # Set default bank object
  #
  # Example:
  # config.default_bank = EuCentralBank.new

  # Add exchange rates to current money bank object.
  # (The conversion rate refers to one direction only)
  #
  # Example:
  # config.add_rate "USD", "CAD", 1.24515
  # config.add_rate "CAD", "USD", 0.803115

  # To handle the inclusion of validations for monetized fields
  # The default value is true
  #
  # config.include_validations = true

  # Default ActiveRecord migration configuration values for columns:
  #
  # config.amount_column = { prefix: '',           # column name prefix
  #                          postfix: '_cents',    # column name  postfix
  #                          column_name: nil,     # full column name     (overrides prefix, postfix and accessor name)
  #                          type: :integer,       # column type
   #                          present: true,        # column will be created
  #                          null: false,          # other options will be    treated as column options
  #                          default: 0
  #                        }
  #
  #config.currency_column = { prefix: '',
  #                         postfix: '_currency',
  #                         column_name: nil,
  #                       type: :string,
  #                   present: true,
  #                    null: false,
  #                   default: 'GBP'
  #                }

  # Register a custom currency
  #
  # Example:
  # config.register_currency = {
  #   :priority            => 1,
  #   :iso_code            => "EU4",
  #   :name                => "Euro with subunit of 4 digits",
  #   :symbol              => "€",
  #   :symbol_first        => true,
  #   :subunit             => "Subcent",
  #   :subunit_to_unit     => 10000,
  #   :thousands_separator => ".",
  #   :decimal_mark        => ","
  # }

  config.register_currency = {
    "priority": 1,
    "iso_code": "GBP",
    "name": "British Pound",
    "symbol": "£",
    "alternate_symbols": [],
    "subunit": "Penny",
    "subunit_to_unit": 100,
    "symbol_first": true,
    "html_entity": "&#x00A3;",
    "decimal_mark": ".",
    "thousands_separator": ",",
    "iso_numeric": "826",
    "smallest_denomination": 1
  }

  config.register_currency = {
    "priority": 2,
    "iso_code": "USD",
    "name": "United States Dollar",
    "symbol": "$",
    "alternate_symbols": ["US$"],
    "subunit": "Cent",
    "subunit_to_unit": 100,
    "symbol_first": true,
    "html_entity": "$",
    "decimal_mark": ".",
    "thousands_separator": ",",
    "iso_numeric": "840",
    "smallest_denomination": 1
  }

  config.register_currency = {
    "priority": 3,
    "iso_code": "EUR",
    "name": "Euro",
    "symbol": "€",
    "alternate_symbols": [],
    "subunit": "Cent",
    "subunit_to_unit": 100,
    "symbol_first": true,
    "html_entity": "&#x20AC;",
    "decimal_mark": ",",
    "thousands_separator": ".",
    "iso_numeric": "978",
    "smallest_denomination": 1
  } 



  # Set default money format globally.
  # Default value is nil meaning "ignore this option".
  # Example:
  #
  # config.default_format = {
  #   :no_cents_if_whole => nil,
  #   :symbol => nil,
  #   :sign_before_symbol => nil
  # }

  # Set default raise_error_on_money_parsing option
  # It will be raise error if assigned different currency
  # The default value is false
  #
  # Example:
  # config.raise_error_on_money_parsing = false
end

show.html.erb

<%= f.collection_select :category_id, Category.all, :id, :name, {prompt: "Choose a category"} %>
<!-- The above code loop assigns a category_id to each event -->

<%= f.input :image, as: :file, label: 'Image' %>
<%= f.input :title, label: 'Event Title' %>
<label>Location</label><%= f.text_field :location, id: 'geocomplete' %></br>
<label>Date</label><%= f.text_field :date, label: 'Date', id: 'datepicker' %>
<%= f.input :time, label: 'Time' %>
<%= f.input :description, label: 'Description' %>
<label>Number of spaces available</label><%= f.text_field :number_of_spaces, label: 'Number of spaces' %>
<%= f.input :is_free, label: 'Tick box if Event is free of charge' %>
<%= f.input :currency, :collection => [['£GBP - British Pounds',1],['$USD - US Dollars',2],['€EUR - Euros',3]] %>
<%= f.input :price, label: 'Cost per person (leave blank if free of charge)' %>
<%= f.input :organised_by, label: 'Organised by' %>
<%= f.input :url, label: "Link to Organiser site" %>

<%= f.button :submit, label: 'Submit' %>

<% end %>   

rails-money自述文件状态建议处理资金的对象 - 在本例中为“价格” - 可以通过迁移进行货币化。在宝石助手工作之前这是强制性的吗?

1 个答案:

答案 0 :(得分:0)

我相信money gem可以覆盖模型中的默认值。由于您希望能够选择货币,因此该宝石可能不适合您的用途。听起来您需要为模型添加货币方法,以便允许用户选择事件货币。这应该是一个简单的迁移。