嗨,我将日期作为 Rails 中的日期数据类型,但我只想将用户日期显示为 mm/yyyy 格式。 这是表单代码,但是当我使用 strftime('%m%y'( 执行此代码时,我收到错误 nil:class,但没有使用 strftime,我得到的输出为 yyyy-mm-dd 格式。 如何以 mm/yy 格式或月-年格式获取输出
表单.html.erb
<%= form_with(model: project_site, local: true) do |form| %>
<% if project_site.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(project_site.errors.count, "error") %> prohibited this project_site from being saved:</h2>
<ul>
<% project_site.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="row">
<div class="field medium-3 columns">
<%= 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 %>
</div>
<div class="field medium-6 columns">
<%= form.label :attendance_month %>
<%= form.date_select :date, { :discard_day => true, :discard_month => false, :discard_year => false } %>
</div>
<div class="actions">
<br>
<%= form.submit 'Upload Attendance', :class => 'button primary'%>
</div>
</div>
<% end %>
显示.html.erb
<%= project_site.date %>
使用strftime
怎么样?
<%= project_site.date.strftime("%m%Y") %>