繁体   English   中英

在.html.erb文件中获取错误的输出

[英]Getting the wrong output in .html.erb file

我正在使用“ active_shipping” gem。 当我在.rb文件中使用以下行时

  puts res.rates.sort_by(&:price).collect 
  {|rate| [rate.service_name,
  (@bank.get_rate(res.rate_estimates[0].currency,:USD)
   *    (rate.price).to_f/100).round(2)]}, 

我得到以下输出:

在此处输入图片说明

但是,何时在.html.erb文件中使用同一行

<%= res.rates.sort_by(&:price).collect 
{|rate| [rate.service_name, 
(@bank.get_rate(res.rate_estimates[0].currency,:USD)
* (rate.price).to_f/100).round(2)]}%>, 

我在视图中得到以下输出:

在此处输入图片说明

有人可以帮我解决这个问题。 如何在.html.erb文件中正确显示文本。 谢谢。

感谢Kathryn,我确实将代码更改为:

<p><%res.rates.sort_by(&:price).each do |rate|%>
 <%=  rate.service_name%>,
 <%=(@bank.get_rate(res.rate_estimates[0].currency,:USD)
  *  (rate.price).to_f/100).round(2)%> </p><br/>
 <%end%>

我得到了期望的输出:

在此处输入图片说明

我认为Ruby对象未在erb中显示,因为您没有足够的定界符。 尝试以下方法:

<% res.rates.sort_by(&:price).collect %> 
<%= {|rate| [rate.service_name, 
  (@bank.get_rate(res.rate_estimates[0].currency,:USD)
  * (rate.price).to_f/100).round(2)]} %> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM