Rails 3.2.11在erb模板中渲染unicode

时间:2013-03-31 22:23:17

标签: ruby-on-rails unicode ruby-on-rails-3.2 erb

我正在尝试在我的erb模板中渲染unicode,例如:

我的种子:

Currency.create!(currency: 'USD', rate: 1.2805, title: 'U.S. Dollars', code: '\u0024', active: 1)

控制器:

@currencies = []
Currency.where(:active => TRUE).each do |node|
   c = node.rate * 25
   c = number_to_currency(c, :precision => 2, :unit => node.code)
   @currencies.push(:currency => node.currency, :price => c)
end

Template.erb:

<select>
    <% @currencies.each do |node| %>
        <option>
            <%= node[:currency] %> - <%= node[:price] %>
        </option>
    <% end %>
</select>

我希望如此:

<option>USD - $32.01</option>

但我得到原始的unicode输出:

<option>USD - \u002432.01</option>

我试过了:

添加utf8编码元属性。添加:&lt;%#encoding:utf-8%&gt;到我的模板。并在字符串上使用.html_safe。

1 个答案:

答案 0 :(得分:1)

这是因为你在种子中使用单引号。它应该是双引号:

"\u0024" # => "$"
'\u0024' # => "\\u0024"