所以我有一个编辑表格,其中包含年份开始和结束日期。我想将db存储的值显示为默认日期。
我试过这个
<%= f.input :year_start_date, :label => 'Year Start Date', :as => :date_select, :include_blank => false,
:input_html => { :id => 'y_start_date'}, prompt: { day: year_start_date.day, month: Date::MONTHNAMES[year_start_date.month], year: year_start_date.year}, data: year_start_date %>
所以很好,并开始显示db存储值。
但问题是当我只改变月份时,参数将是
"year_start_date(1i)" => "", "year_start_date(2i)" => "3", "year_start_date(3i)" => ""
并且在控制器中正在做
[ school_params['year_start_date(1i)'], school_params['year_start_date(2i)'], school_params['year_start_date(3i)'] ].join('-').to_date
因为它包含空字符串,所以它会抛出无效的日期错误。
当没有选择任何内容时,我想要默认值(在本例中为日期和年份)。怎么做到这一点?
编辑1:
params
{
"utf8" => "✓",
"_method" => "patch",
"authenticity_token" => "abcffdgfdgfgfgf==",
"school" => {
"name" => "Test School",
"board_id" => "1",
"board_registration_number" => "",
"subdomain" => "testschool",
"email" => "",
"website" => "",
"address" => "NA",
"city" => "NA",
"pincode" => "",
"country_id" => "1",
"country_state" => "Tamil Nadu",
"phone" => "NA",
"inc_year" => "",
"handler_id" => "14451",
"year_start_date(1i)" => "",
"year_start_date(2i)" => "1",
"year_start_date(3i)" => "",
"year_end_date(1i)" => "",
"year_end_date(2i)" => "",
"year_end_date(3i)" => ""
},
"button" => "",
"controller" => "sat/schools",
"action" => "update",
"id" => "2"
}
答案 0 :(得分:0)
您可以简单地使用浏览器提供的日期选择器,而无需添加额外的库,而不是使用丑陋的日期/日期时间输入:
f.input :year_start_date, :label => 'Year Start Date', as: :string, input_html: { type: "date" }