Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to get symbol from money gem
    text
    copied!<p>I am using money gem to represent money fields in my Product model. Here's my code:</p> <p><strong>Product model:</strong></p> <pre><code> composed_of :price, :class_name =&gt; "Money", :mapping =&gt; [%w(price cents), %w(currency currency_as_string)], :constructor =&gt; Proc.new { |cents, currency| Money.new(cents || 0, currency || Money.default_currency) }, :converter =&gt; Proc.new { |value| value.respond_to?(:to_money) ? value.to_money : raise(ArgumentError, "Can't convert #{value.class} to Money") } </code></pre> <ul> <li>I have fields like currency as string and price as string in my model.</li> </ul> <p>I my <strong>form view</strong>:</p> <pre><code> &lt;li&gt; &lt;%= f.select(:currency,all_currencies(Money::Currency::TABLE), {:include_blank =&gt; 'Select a Currency'}, :class =&gt; "styled" )%&gt; &lt;/li&gt; </code></pre> <p>In my <strong>Products helper</strong></p> <pre><code>def all_currencies(hash) hash.inject([]) do |array, (id, attributes)| array ||= [] array &lt;&lt; [attributes[:name], attributes[:iso_code]] array end end </code></pre> <ul> <li>When I try to print product.currency in my view, I get the correct currency.For example for indian rupees, I get INR.But I dont know how to get symbol out of it.</li> <li>I tried using product.symbol as well as product.currency.symbol but I get "undefined method symbol"</li> </ul> <p>Any help is deeply appreciated.</p> <p>=====Solution:====</p> <p>Inside my helper method,I needed to get [:symbol] attribute in the array..</p> <pre><code>def all_currencies(hash) hash.inject([]) do |array, (id, attributes)| array ||= [] array &lt;&lt; [attributes[:name], attributes[:iso_code],attributes[:symbol]] array end end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload