Hi,
I render a couple of date_select fields, the HTML looks fine, I can
see the values submitted back to the server, but AR doesn’t pick them
up from the hash in Booking.new(params[‘booking’]).
I’ve got a ‘booking’ object with parameters product, startdate and
enddate.
HTML snippets:
…
…
…
In the view controller method I create a booking object and set
startdate to Date.today+1, and enddate to Date.today+2. When the
page renders the dates are showing correctly according to how the
controller set them up, so date_select is showing the right fields
from my model.
I’ve tried different browsers, and I’ve tried gem rails and edge rails.
From the console log I can see the values:
Processing SiteController#booking (for 127.0.0.1 at 2006-02-08
10:19:25) [POST]
Parameters: {“commit”=>“Check Availability”, “action”=>“booking”,
“id”=>“1”,
“controller”=>“site”,
“booking”=>{“startdate(1i)”=>“2006”, “startdate(2i)”=>“2”,
“startdate(3i)”=>“9”,
“enddate(1i)”=>“2006”, “enddate(2i)”=>“2”, “enddate(3i)”=>“10”}}
But the save always fails and the view reports the errors like so:
2 errors prohibited this booking from being saved
There were problems with the following fields:
* Enddate can’t be blank
* Startdate can’t be blank
The form looks like this:
<%=error_messages_for(“booking”)%>
<%=form_tag({:action => ‘booking’, :id=>@product})%>
Start:
<%=date_select("booking", "startdate")%>
End:
<%=date_select("booking", "enddate")%>
There are no methods in the model:
class Booking < ActiveRecord::Base
belongs_to :product
validates_presence_of :startdate, :enddate, :product
end
The controller method is quite simple:
def booking
@product = Product.find(params[:id])
@booking = Booking.new(params[‘booking’])
@booking.product = @product
# I added these 3 lines so I could dump them
# in the view to try and debug the problem:
@startdate = params['booking']['startdate']
@enddate = params['booking']['enddate']
@params = params
if @booking.save
redirect_to(:action=>'index')
else
render(:action=>'view')
end
end
I dump @startdate, @enddate and @params in my view and get:
Startdate:
Enddate:
Params:
— !map:HashWithIndifferentAccess
commit: Check Availability
action: booking
id: “1”
controller: site
booking: !map:HashWithIndifferentAccess
startdate(1i): “2006”
startdate(2i): “2”
startdate(3i): “9”
enddate(1i): “2006”
enddate(2i): “2”
enddate(3i): “10”
The database table has all the fields:
CREATE TABLE bookings
(
id
int(11) NOT NULL auto_increment,
product_id
int(11) default NULL,
startdate
datetime default NULL,
enddate
datetime default NULL,
created_at
datetime default NULL,
updated_at
datetime default NULL,
PRIMARY KEY (id
),
KEY bookings_product_id_index
(product_id
),
KEY bookings_startdate_index
(startdate
),
KEY bookings_enddate_index
(enddate
)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
(they were originally ‘date’ fields, I changed them to ‘datetime’
while trying to get this working).
Any help would be appreciated!
Jon