I’ve got this basic model, where a provision object, a nested resource of a bill, can have three types of effective date types. Suffice to say, if the client picks the third option “On a specific date”, I got a bit of JQuery popping up a second field with a datepicker so he can select that datetime value.
However, for the past few hours I’ve been pulling my hair trying to figure out why the server receives the parameters correctly, but enters the value for the datetime as nil. I’ve tried every single trick in the book to resolve this (even tried dropping the column and recreating it). I’m sure I’m missing something quite obvious. Please help 🙁
Here’s my code, let’s start
first with the Server response (the parameter in question is called date_of_effect):
Started POST "/bills/1/provisions" for 127.0.0.1 at 2011-08-18 22:27:42 -0400
Processing by ProvisionsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"v6B+sgSsOsPXOGl2TulguIpfXaRmvTBzUJ7Qge/aNhM=", "provision"=>{"article"=>"2", "text"=>"testing this", "in_effect"=>"3", "date_of_effect"=>"08/25/2011"}, "commit"=>"Create Provision", "bill_id"=>"1"}
SQL (0.6ms) INSERT INTO "provisions" ("article", "bill_id", "created_at", "date_of_effect", "in_effect", "order_id", "text", "updated_at") VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["article", 2], ["bill_id", 1], ["created_at", Fri, 19 Aug 2011 02:27:42 UTC +00:00], ["date_of_effect", nil], ["in_effect", 3], ["order_id", nil], ["text", "testing this"], ["updated_at", Fri, 19 Aug 2011 02:27:42 UTC +00:00]]
Bill Load (0.1ms) SELECT "bills".* FROM "bills" WHERE "bills"."id" = 1 LIMIT 1
Redirected to http://localhost:3000/bills/1
Completed 302 Found in 230ms
The relevant fields of my view look like this:
<div class="datelabel">Select a date</div>
<div class="datelabel"><%= f.text_field(:date_of_effect) %></div>
…
<% content_for :bottom do %>
<script>
$(function() {
$(".datelabel").hide();
$('#provision_date_of_effect').datepicker();
$('#provision_in_effect').change(function() {
($(this).val() == '3') ? $('.datelabel').show('puff') : $('.datelabel').hide();
});
});
</script>
<% end %>
And finally my create method in the controller (notice the comment lines are me trying out different methods to get the datetime to stick, to no avail unfortunately):
def create
@provision = Provision.new(params[:provision])
@provision.bill_id = params[:bill_id]
@provision.save
# @provision.date_of_effect = params[:provision][:date_of_effect]
# @provision.save(:validate => false)
flash[:notice] = "Section added"
redirect_to @provision.bill
end
Any help would be greatly appreciated, thanks 🙂
You can do this:
Or you can change format for datepicker. Date can’t parse this format “08/25/2011”