2011-06-16 29 views
1

我一直在試圖遵循這一Railscast:http://railscasts.com/episodes/213-calendars更換導軌date_select使用jQuery UI

我已經實現了控制,所有的javscript工作...它加載日期到字段,它與提交params,但rails仍然在NULL字段中插入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(); 
}); 

有什麼想法?

回答

2

生成的daate的格式,即「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

我也能夠更新jquery:'dateFormat:'yy-mm-dd'' – Slick23 2011-06-16 21:19:58

0

您是否將shift_on字段存儲爲日期?如果你需要先用Date.parse解析它,否則你試圖在那裏保存一個字符串。