编辑视图中的Rails日期格式 [英] Rails Date Format in edit view
问题描述
我正在Rails中创建Edit表单,并使用partial进行新建和编辑 我正在使用日期选择器jquery,因此用户可以使用jquery-ui选择日期
I'm creating Edit form in rails using partial for new and edit I'm using date picker jquery so user can choose date with jquery-ui
我的问题是如何格式化所有日期字段 因为在编辑表单中,所有内容都显示为"YYYY-MM-DD" 我想显示"dd-mm-yyyy"
my problem is how do I format all my date fields because in edit form it's all showing 'YYYY-MM-DD' I would like it's showing 'dd-mm-yyyy'
例如,预订日期,它显示的是2014-04-20,我希望2014年4月20日
for example booking-date, it's showing 2014-04-20, I would like 20-04-2014
我确实改变了 config/initializers/date_formats.rb 日期:: DATE_FORMATS [:默认] =%d-%m-%Y" 它只能以显示形式工作,而不能在编辑"模式下工作
I did change config/initializers/date_formats.rb Date::DATE_FORMATS[:default] = "%d-%m-%Y" it's working only in show form but not in Edit mode
也在Rails控制台中(使用调试器)我检查日期格式为 'YYYY-MM-DD',有没有办法更改它
also in rails console (using debugger) I check the date format is 'YYYY-MM-DD' , is there a way to change it
下面供参考的是我的show.html.erb和edit.html.erb(部分)
for reference below is my show.html.erb and edit.html.erb (partial)
show.html.erb
show.html.erb
<% if logged_in? %>
<p>Booking Date <br> <%= @booking.booking_date %> </p>
edit.html.erb
edit.html.erb
<%= form_for @booking do |f| %>
<%= render 'common/form_errors', object: @booking %>
<%= f.label :booking_date, "Booking Date" %><br>
<%= f.text_field :booking_date, :class => :datepicker, size: 10 %>
推荐答案
如果表单是编辑的,则可以手动添加日期值.
You could add date value manually if the form is edit.
=@booking.booking_date.striftime("%d-%m-%YYYY")
2014年4月20日
20-04-2014
edit.html.erb
edit.html.erb
<%= form_for @booking do |f| %>
<%= render 'common/form_errors', object: @booking %>
<%= f.label :booking_date, "Booking Date" %><br>
<%= f.text_field :booking_date, :class => :datepicker, size: 10, :value=>"#{f.object.booking_date.striftime("%d-%m-%YYYY") unless f.object.new_record?}" %>
这篇关于编辑视图中的Rails日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!