【问题标题】:Issue using .erb to display date with Sinatra使用 .erb 与 Sinatra 显示日期的问题
【发布时间】:2017-07-10 17:23:25
【问题描述】:

我正在使用 ruby​​ .erb 在 div 的跨度中显示时间。但是,对日期使用类似的东西是行不通的。

<div id="comment_space">
  <% @post.comments.each_with_index do |comment,date,posted,index| %> 
    <span class="above"><%= date = ('%Y-%m-%d') %> </span> 
    <div   class="comment"     id="msg_<%= index+1 %>"> 
      <%= comment.usr_comment %>
      <span class="right">
        <%= comment.created_at.localtime.strftime("%l: %M %p") %>
      </span> 
    </div>
  <% end %>
  <span class="above">
    <%= date = ('%Y-%m-%d') %> 
  </span> 

这是不工作的部分。不过

<span class="right">
  <%= comment.created_at.localtime.strftime("%l: %M %p") %>
</span>

完美运行。

第一个错误是“created_at”没有被锁定为日期,它从那里走下坡路。日期和时间在 Javascript 中定义,时间戳在 Ruby ActiveRecord 表中。

【问题讨论】:

  • 请不要在标题中写问题。找到合适的标题并将问题放在正确的位置。
  • @ed 感谢您的提示!仍然对网站有所了解,感谢您的反馈。

标签: ruby sinatra erb


【解决方案1】:
<%= date = ('%Y-%m-%d') %>  # Ehhhhhm

好的,将其与您的其他方式进行比较。

<%= comment.created_at.localtime.strftime("%l: %M %p") %>

看到区别了吗? 如果“日期”是 Date 类,你可以这样做:

<%= date.strftime('%Y-%m-%d') %>

它应该没有任何问题。

如果你做了你所做的,你说日期现在是'%Y-%m-%d'

date = ('%Y-%m-%d')
# => "%Y-%m-%d"

【讨论】:

  • 很好的建议,非常感谢它现在完美运行!
猜你喜欢
  • 2015-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-03
  • 2019-01-19
  • 1970-01-01
  • 2021-06-11
  • 2015-09-06
相关资源
最近更新 更多