2013-03-31 115 views
0

我試圖使統一在我的ERB模板,如3.2.11渲染的Unicode:的Rails在ERB模板

我的種子:

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 

模板.erb:

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

I e xpect這樣的:

<option>USD - $32.01</option> 

,但我得到的原始的unicode輸出:

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

我已經試過:

添加utf8編碼元屬性。將<%#encoding:utf-8%>添加到我的模板中。並在字符串上使用.html_safe。

回答

1

這是因爲你在你的種子中使用單引號。它應該是雙引號:

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