Moneyrails宝石-如何高估默认货币并显示小数点



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

这是我现在的代码-

Event.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

money.rb

    # 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

_form.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 %>   

show.html.erb

<%= image_tag @event.image.url %>
<h1><%= @event.title %></h1>
<p>Location </p>
<p><%= @event.location %></p>
<p>Date</p>
<p><%= @event.date.strftime('%A, %d %b %Y') %></p>
<p>Time</p>
<p><%= @event.time.strftime('%l:%M %p') %></p>
<!-- above expresses date and time as per UK expectations -->
<p>More details</p>
<p><%= @event.description %></p>
<p>Number of Spaces available</p>
 <p><%= @event.number_of_spaces %></p>
<% if @event.is_free? %>
  <p>This is a free event</p>
<% else %>
<p>Cost per person</p>
<p><%= humanized_money_with_symbol @event.price %></p>
<% end %>
<p>Organiser</p>
<p><%= @event.organised_by %></p>
<p>Organiser Profile</p>
<button><%= link_to "Profile", user_path(@event.user) %></button>
<p>Link to Organiser site</p>
<button><%= link_to "Organiser site", @event.url %></button>
<p>Submitted by</p> 
<p><%= @event.user.name %></p>

<% if user_signed_in? and current_user == @event.user %>
<%= link_to "Edit", edit_event_path %>
<%= link_to "Delete", event_path, method: :delete, data: { confirm: "Are you   sure?"} %>
<%= link_to "Back", root_path %>
<% else %>
<%= link_to "Back", root_path %>
<%= link_to "Book the Event", new_event_booking_path(@event) %>
<% end %>

rails money ReadMe文件建议处理金钱的对象(在本例中为"price")可以通过迁移"货币化"。在gem助手工作之前,这是强制性的吗?

我相信money gem可以让您覆盖模型中的默认值。既然你想选择货币,这个宝石可能不适合你的目的。听起来您需要在模型中添加一个货币方法,以便用户选择事件货币。这应该是一个简单的迁移。

最新更新