2015-11-03 49 views
0

我的應用程序出現問題。我正在做一個簡單的CMS,我需要顯示職位的創建日期。我現在正在做的事情無法正常工作。它正確顯示時間,但不是日期。對於每一個條目,它都表明日期是10月20日,但它不是。Rails顯示對象的created_at的錯誤日期和updated_at

我通過rails c檢查了我的分貝,它存儲了正確的值。所以這個問題必須在顯示代碼的地方。

下面是我的輸出是:

<% @articles.each do |article| %> 
    <h3 class="article-title"><u><%= link_to article.title, article %></u></h3> 
    <p><%= article.text.html_safe %></p> 
    <div class="article-info"> 
    <p>Posted on <em><%= article.updated_at.strftime("%A, %C %B %G %R") %> </em> by <em><%= article.user.username %></em> | 
    <%= link_to 'Comments...', article %> 
    <% if user_signed_in? and article.user == current_user %> 
     | <%= link_to 'Edit', edit_article_path(article) %> 
     | <%= link_to 'Destroy', article, method: :delete, data: { confirm: 'Are you certain you want to delete this?' } %> </p> 
    <% end %> 
    </div> 
    <% if article.tag_list.any? %> 
     <div class="article-tags">Tags: <%= raw article.tag_list.map { |tag| link_to tag, tag_path(tag)}.join(", ") %></div> 
    <% end %> 
    <hr /> 
<% end %> 

哪些原因會導致這樣的行爲?

+2

閱讀有關的strftime並給我們您的格式 「%A,%C%B%G%R」 解釋 – Meier

+0

@Meier哦,我現在看到。我認爲'%C'是一個月中的一天,但實際上是一年除以100。它應該是'%d'。 – serge1peshcoff

回答

1

你的問題是strftime的屬性:

<%= article.updated_at.strftime("%A, %C %B %G %R") %> 

%A - 完整的平日名稱(Sunday'') %C - year/100 (round down. 20 in 2009) %B - The full month name (一月 '') %G - 基於一週年%R - 24小時的時間( %H:%M)

您需要使用這樣的:

<%= article.updated_at.strftime("%A, %d %B %Y %R") %> 

%d - %d - 月份,零填充日(01..31)

%Y - 與世紀

APIdock - DateTime

1

使用這種格式在你的代碼新年

<%= article.updated_at.strftime("%A, %d %B %Y %l:%M%p") %> 
相關問題