我一直在尝试关注此Railscast:http://railscasts.com/episodes/213-calendars
我已经实现了控件并且所有javscript都在工作......它会在字段中加载日期并使用params提交,但rails仍然在日期字段中插入NULL。这是日志显示的内容:
Started POST "/shifts" for 127.0.0.1 at 2011-06-16 12:52:24 -0400
Processing by ShiftsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"W1MmVDoKMuR3HKmql+dDdWXt70xmZ9bVOJgdUtb1lKQ=", "shift"=>{"shift_on"=>"06/23/2011", "state"=>"requested"}, "commit"=>"Create Shift"}
SQL (0.3ms) SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
AREL (0.4ms) INSERT INTO "shifts" ("user_id", "shift_on", "state", "created_at", "updated_at") VALUES (NULL, NULL, 'requested', '2011-06-16 16:52:24.743149', '2011-06-16 16:52:24.743149')
Redirected to http://localhost:3000/shifts/1
Completed 302 Found in 274ms
shift_on
coloumn是日期类型。
这是我的表格:
<%= form_for(@shift) do |f| %>
<% if @shift.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@shift.errors.count, "error") %> prohibited this shift from being saved:</h2>
<ul>
<% @shift.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :shift_on %><br />
<%= f.text_field :shift_on %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
这是我的application.js:
$(function() {
$("#shift_shift_on").datepicker();
});
有什么想法吗?
答案 0 :(得分:2)
生成的日期格式即“06/23/2011”无法识别,因此会插入空值。日期应采用可识别的格式,例如“2011-6-23”等。
要解决此问题,您可以将收到的日期参数shift_on更改为红宝石日期,
Date.strptime(shift_on, "%m/%d/%Y")
在这里,
Date.strptime("06/23/2011", "%m/%d/%Y") = Thu, 23 Jun 2011 which can then be stored in the Db.
答案 1 :(得分:0)
您是否将shift_on字段存储为日期?如果你需要先用Date.parse解析它,否则你会尝试在那里保存一个字符串。