# Date\_select problem

**URL:** <https://www.ruby-forum.com/t/date-select-problem/147166>\
**Category:** Rails\
**Created:** [August 28, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/date-select-problem/147166 "2008-08-28T23:36:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![billv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@billv](https://www.ruby-forum.com/u/billv)\
**Post date:** [August 28, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/date-select-problem/147166/1 "2008-08-28T23:36:57Z")

</div>

Hi,

I’m entering two dates into a view through the date\_select helper, the  
first relates to a field in the database, the second has been added to  
the model with attr\_accessor. When the create action is invoked, it  
fails when trying to create a new object. Here’s the code:

View

\<% form\_for(@cto) do |f| %\>

\<%= f.date\_select :dayoff %\>

\<%= f.date\_select :end\_date %\>

\<%= f.submit "Create" %\>

\<% end %\>

Controller

def create  
@cto = Cto.new(params[:cto])  
respond\_to do |format|  
if @cto.save  
flash[:notice] = “Cto was successfully created.}”  
format.html { redirect\_to(@cto) }  
format.xml { render :xml =\> @cto, :status  
=\> :created, :location =\> @cto }  
else  
format.html { render :action =\> “new” }  
format.xml { render :xml =\> @cto.errors, :status  
=\> :unprocessable\_entity }  
end  
end  
end

Model

class Cto \< ActiveRecord::Base

attr\_accessor :end\_date

end

Error

You have a nil object when you didn’t expect it!  
The error occurred while evaluating nil.klass

Parameters

{“commit”=\>“Create”,  
“authenticity\_token”=\>“1b2c46eed03c8154cc26d32fa8b01d6e4caa4490”,  
“cto”=\>{“end\_date(3i)”=\>“28”,  
“dayoff(1i)”=\>“2008”,  
“dayoff(2i)”=\>“8”,  
“dayoff(3i)”=\>“28”,  
“end\_date(1i)”=\>“2008”,  
“end\_date(2i)”=\>“8”}}

Any ideas? If I change the date\_select to a select\_date, the error  
stops, but then I don’t have the data in my model.

Sorry for the long message, just thought the code would help.

---

<div class="post-metadata">

**Author:** ![billv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@billv](https://www.ruby-forum.com/u/billv)\
**Post date:** [August 29, 2008, 12:26am UTC](https://www.ruby-forum.com/t/date-select-problem/147166/2 "2008-08-29T00:26:09Z")

</div>

On 28 Aug 2008, at 22:36, billv wrote:

> You have a nil object when you didn’t expect it!  
> “end\_date(1i)”=\>“2008”,  
> “end\_date(2i)”=\>“8”}}
> 
> Any ideas? If I change the date\_select to a select\_date, the error  
> stops, but then I don’t have the data in my model.

Long version: This is due to the slightly magic way in which  
date\_select works. As you can see you get three parameters. The (1i),  
(2i) etc… suffices mean ‘this one is the nth parameter to the  
constructor and it’s an integer’. Rails then looks at your model, sees  
that dayoff is a date column and knows that it should creating a new  
Date, passing the 3 2008,8 and 28 as arguments.  
It can’t do this trick with end\_date because it has no way of knowing  
what kind of object it should be creating.  
Short version: use select\_date (you’ll have to take the appropriate  
parameters and turn them into a date yourself.

Fred
