[英]Formating variable within a string interpolation
I have a string interpolation that looks like this: 我有一个字符串插值看起来像这样:
<ul class="list-group">
<% @top_ten_cashflows.each do |transaction|%>
<li class="list-group-item"> <%=" #{ transaction.date.format('%b') } - #{transaction.category.name} - #{transaction.amount}"%></li>
<%end%>
</ul>
Rails brings up the following error when i try to format the date: 当我尝试格式化日期时,Rails会出现以下错误:
NoMethodError at /Dashboard
private method `format' called for Wed, 20 Jul 2016:Date
The controller looks as follows: 控制器如下所示:
def dashboard
#@cashflows = current_user.cashflows
# cashflows_sums =@cashflows.inject do |sum,element|
@cashflows = Cashflow.where(user_id: current_user.id).joins(:category)
@top_ten_cashflows = @cashflows.order(:date).limit(10)
end
How do i get the date to be formatted within the string interpolation with the day and short month. 我如何获取要在日期内插入带日期和短月份的字符串插值格式的日期。
transaction.date.strftime('%b')
Even better, use I18n localization Date/Time formats , it is easy, flexible and adds an extra level of customization per locale. 更好的是,使用I18n本地化日期/时间格式 ,它简单,灵活,并为每个语言环境增加了额外的自定义级别。
# app/views/home/index.html.erb
<p><%= l Time.now, format: :short %></p>
You can use the default Rails Date/Tme formats or add your custom formats. 您可以使用默认的Rails Date / Tme格式,也可以添加自定义格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.