私が構築しているイベントアプリのための初めてのレールマネーの宝石を使用しています。私は整数型の '価格'列を持つイベントモデルを持っています。私は、ユーザーがイベントの費用の理由で(理由の如何を問わず)どんなバリエーションでも入力できるようにしたいと思います。たとえば、15.99ドル、15.99ドルなどですが、ショーの出力はきちんとして適切である必要があります(15.99ポンド)。現時点では、私の入力フィールドはフォームに小数点を置くことができますが、ショーページではこれを認識しません(15.99ポンドは15として表示されます)。また、3つの主要通貨を持つ通貨選択フィールドが選択肢£/€/ $として表示されますが、ショーページで何を選択したとしても、上記の例では15.99ドルが$ 15として表示されます。これをどうやって解決するのですか?マネーレールの宝石 - デフォルトの通貨を上書きして小数点を表示する方法
これは、現時点で私が持っているコードです -
<%= 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 %>
_form.html.erb
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": "£",
"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": "€",
"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
<%= 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ファイルの状態では、お金を処理するオブジェクト(この例では「価格」)をマイグレーションによって収益化することが推奨されています。宝石ヘルパーが機能する前に必須ですか?
こんにちは、私は通貨オプションを選別して、ユーザーは3つのうちの1つを選択できますが、ショーページではデフォルト($)に戻ります。 –