有关如何将日期格式化为mm/dd/yy
的任何建议?此选项不起作用:
$('#datepicker').datepicker({ dateFormat: 'dd-mm-yy' });
我遇到的问题是在保存日期之后我又回去编辑它,它将日期显示为1901,因为它正在解析2016-01-01。如何将日期作为mm/dd/yy
存储在数据库中?好像我需要将格式添加到<%= f.text_field :jobdate, "data-provide" => 'datepicker', class: "form-control" %>
class AppointmentsController < ApplicationController
before_action :set_appointment, only: [:show, :edit, :update, :destroy]
# GET /appointments
# GET /appointments.json
def index
@appointments = Appointment.all
end
# GET /appointments/1
# GET /appointments/1.json
def show
end
# GET /appointments/new
def new
@appointment = Appointment.new
end
# GET /appointments/1/edit
def edit
end
# POST /appointments
# POST /appointments.json
def create
@appointment = Appointment.new(appointment_params)
respond_to do |format|
if @appointment.save
format.html { redirect_to @appointment, notice: 'Appointment was successfully created.' }
format.json { render action: 'show', status: :created, location: @appointment }
else
format.html { render action: 'new' }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /appointments/1
# PATCH/PUT /appointments/1.json
def update
respond_to do |format|
if @appointment.update(appointment_params)
format.html { redirect_to @appointment, notice: 'Appointment was successfully updated.' }
format.json { render action: 'show', status: :ok, location: @appointment }
else
format.html { render action: 'edit' }
format.json { render json: @appointment.errors, status: :unprocessable_entity }
end
end
end
# DELETE /appointments/1
# DELETE /appointments/1.json
def destroy
@appointment.destroy
respond_to do |format|
format.html { redirect_to appointments_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_appointment
@appointment = Appointment.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def appointment_params
params.require(:appointment).permit(:First_Name, :last_name, :phone, :cellphone_yes, :cell_provider, :jobdate, :time_start, :time_stop, :status, :location, :service, :sales_person, :technician, :year, :make, :model, :vehicle_type, :cost, :glass_number, :glass_ordered, :portland, :pilkington, :glass_type, :glass_type_other, :how_paid, :paid_type, :insurance, :insurance_company, :vin, :referral, :heated_wiper, :rain_sensor, :condensation_sensor, :electrochromatic, :lane_departure, :molding_needed, :molding_number, :molding_ordered, :other_phone, :work_business_name, :address1, :address2, :city, :state, :zip, :notes)
end
end
答案 0 :(得分:0)
jQuery datepicker会将日期作为文本发送回rails应用程序;一旦您对屏幕上显示日期的方式感到满意,您可以使用strptime
在控制器中正确解析它。在您的情况下,如果您想要mm/dd/yy
,您只需更改收到的参数,如下所示:
def appointment_params
original_params = params.require(:appointment).permit(:First_Name, :last_name, :phone, :cellphone_yes, :cell_provider, :jobdate, :time_start, :time_stop, :status, :location, :service, :sales_person, :technician, :year, :make, :model, :vehicle_type, :cost, :glass_number, :glass_ordered, :portland, :pilkington, :glass_type, :glass_type_other, :how_paid, :paid_type, :insurance, :insurance_company, :vin, :referral, :heated_wiper, :rain_sensor, :condensation_sensor, :electrochromatic, :lane_departure, :molding_needed, :molding_number, :molding_ordered, :other_phone, :work_business_name, :address1, :address2, :city, :state, :zip, :notes)
original_params.merge({jobdate: Date.strptime(original_params.delete(:jobdate), '%m/%d/%y')})
end
您还应该将正确格式化的日期发送到表单。要做到这一点,最简单的可能只是设置字段值而不是使用格式:
<%= f.text_field :jobdate, "data-provide" => 'datepicker', :value => (@model_instance.jobdate || Date.today).strftime("%m/%d/%y") %>
您还必须与jQuery datepicker配置中的格式保持一致:
$( "#datepicker" ).datepicker({
dateFormat: "mm/dd/yy"
});