无法在 rails 中以 Month_name-yyyy 格式打印日期

not able to print date in Month_name-yyyy format in rails

您好,我正在以 mm-yyyy 格式输入日期我想以 month_name-yyyy 格式打印日期。我使用过 strftime,但我无法按照我需要的格式打印日期。

index.html.erb代码

          <td><%= project_site.name.titleize %></td>
          <td><%= project_site.created_at.strftime('%b-%Y') %></td>
          <td><%= link_to ' View attendance', project_site.file, :class => "fi-page-export-csv" %></td>
          <% project_site.manager_remarks.each do |manager_remark| %>
            <% if manager_remark.decision == false %>
              <td><%= 'Rejected' %></td>
            <% elsif manager_remark.decision == true %>
              <td><%= "Approved" %></td>
            <% else %>
              <td><%= "Pending" %>
            <% end %>
         <% end %>
         <td><%= project_site.attendance_month.strftime('%b %Y') %></td>
          <td><%= link_to 'Remark ', project_site %><span>(<%= project_site.manager_remarks.size %>)</span></td>
          <td><%= link_to 'Edit', edit_project_site_path(project_site) %></td>
          <td><%= link_to 'Delete', project_site, method: :delete, data: { confirm: 'Are you sure?' } %></td>
        </tr>

form.html.erb代码

        <%= form.label :name %>
        <%= form.text_field :name %>
      </div>
  <!--
      <div class="field medium-3 columns">
        <%= form.label :date %>
        <%= form.text_field :date, class: 'datepicker' %>
      </div>
  -->
        <div class="field medium-3 columns">
          <%= form.label :upload_attendance %>
          <%= form.file_field :file, :class=> 'attendance-file' %>
        </div>

        <div class="field medium-6 columns">
          <%= form.label :attendance_month %>
          <%= form.date_select :attendance_month, { :discard_day => true, :discard_month => false, :discard_year => false },:class => 'datetime' %>
        </div>

https://www.foragoodstrftime.com/应该能帮到你

尝试:

<td><%= project_site.attendance_month.strftime('%B %Y') %></td>

%B表示strftime中月份的全称

试试这个代码片段。

<td><%= project_site.created_at.strftime('%B-%Y') %></td>