date_select 表单失败,显示“<field> 无效”
date_select in form fails with "<field> is invalid"
我有这个模板,
<div class="form-group">
<label>Insurance Expiry : </label>
<%= date_select f, :insurance_expiry, class: "form-control" %>
</div>
迁移有,
def change do
create table(:buses) do
# snipped
add :insurance_expiry, :date, default: Ecto.Date.local
end
在数据库模型中,
schema "buses" do
# snipped
field :insurance_expiry, :date
end
有关创建操作的调试信息,
[info] Processing by BusMan.BusController.create/2
Parameters: %{"_csrf_token" => "PDkIZycHTRJsEzwOEBJRXxo6MVIFJgAAHla/FI4Y5PQxTYdk/XakNg==", "_utf8" => "✓", "bus" => %{"bus_no" => "138", "chassis_no" => "nTHSNTH", "engine_no" => "RCHR989", "insurance_expiry" => %{"day" => "1", "month" => "10", "year" => "2019"}, "models" => "NTHRCG898", "reg_no" => "TN21W0613", "year_of_registration" => "1990"}, "format" => "html"}
表单提交失败:
Oops, something went wrong! Please check the errors below:
Insurance expiry is invalid
我只想输入一个日期, date_select
是我需要的还是我遗漏了什么?
如 José Valim 更正,使用 Ecto.Date
而不是 :date
正确解决了问题,并且不需要显式转换。
看起来传递给创建函数的日期是一张地图。在插入它之前,尝试将其转换为
的日期
Ecto.Date.cast(%{"day" => "1", "month" => "10", "year" => "2019"})
我可以想象代码看起来像
selected_date = %{"day" => "1", "month" => "10", "year" => "2019"}
|> Ecto.Date.cast
|> Repo.insert!
我有这个模板,
<div class="form-group">
<label>Insurance Expiry : </label>
<%= date_select f, :insurance_expiry, class: "form-control" %>
</div>
迁移有,
def change do
create table(:buses) do
# snipped
add :insurance_expiry, :date, default: Ecto.Date.local
end
在数据库模型中,
schema "buses" do
# snipped
field :insurance_expiry, :date
end
有关创建操作的调试信息,
[info] Processing by BusMan.BusController.create/2
Parameters: %{"_csrf_token" => "PDkIZycHTRJsEzwOEBJRXxo6MVIFJgAAHla/FI4Y5PQxTYdk/XakNg==", "_utf8" => "✓", "bus" => %{"bus_no" => "138", "chassis_no" => "nTHSNTH", "engine_no" => "RCHR989", "insurance_expiry" => %{"day" => "1", "month" => "10", "year" => "2019"}, "models" => "NTHRCG898", "reg_no" => "TN21W0613", "year_of_registration" => "1990"}, "format" => "html"}
表单提交失败:
Oops, something went wrong! Please check the errors below:
Insurance expiry is invalid
我只想输入一个日期, date_select
是我需要的还是我遗漏了什么?
如 José Valim 更正,使用 Ecto.Date
而不是 :date
正确解决了问题,并且不需要显式转换。
看起来传递给创建函数的日期是一张地图。在插入它之前,尝试将其转换为
的日期Ecto.Date.cast(%{"day" => "1", "month" => "10", "year" => "2019"})
我可以想象代码看起来像
selected_date = %{"day" => "1", "month" => "10", "year" => "2019"}
|> Ecto.Date.cast
|> Repo.insert!