# Date in a textfield

**URL:** <https://www.ruby-forum.com/t/date-in-a-textfield/186164>\
**Category:** Rails\
**Created:** [April 1, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/date-in-a-textfield/186164 "2010-04-01T14:10:13Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 1, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/date-in-a-textfield/186164/1 "2010-04-01T14:10:13Z")

</div>

Hi  
I have two date fields in table start\_date and end\_date .There data  
type are datetime. Now in the view I am accepting values of them as  
text fields like And I am following RailsCast 32

\<%= f.text\_field :start\_date\_string, :value =\> ‘’, :maxlength =\> 50  
%\> ![Calendar]()  
\<%= f.text\_field :end\_date\_string, :value =\> ‘’, :maxlength =\> 50 %\>

And in my model

validates\_presence\_of :start\_date

def start\_date\_string  
start\_date.to\_s(:db)  
end

def start\_date\_string=(start\_date\_str)  
self.start\_date = Time.parse(start\_date\_str)  
end

def end\_date\_string  
end\_date.to\_s(:db)  
end

def end\_date\_string=(end\_date\_str)  
self.end\_date = Time.parse(end\_date\_str)  
end

But my problem is validates\_presence\_of :start\_date is not working  
Even if I dont give any values to start date it does not show any error  
message. Please help

Thanks in advance  
Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 1, 2010, 2:51pm UTC](https://www.ruby-forum.com/t/date-in-a-textfield/186164/2 "2010-04-01T14:51:20Z")

</div>

On 1 April 2010 13:10, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> And in my model
> 
> Even if I dont give any values to start date it does not show any error  
> message. Please help

When you say it is not working, what exactly is not working? Two  
things should happen if the validate fails, firstly the record should  
not get saved to the database and secondly an error message should be  
made available. You have said that you are seeing no message but is  
the record getting saved or not?

How are you showing the error message? An error here could be the  
explanation for not seeing the message.

Have you tried using ruby-debug to break into your controller and see  
what is happening when you submit? If you do not know how to do this  
have a look at the rails guide on debugging at  
[http://guides.rubyonrails.org/](http://guides.rubyonrails.org/).

Colin

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 14, 2010, 12:58pm UTC](https://www.ruby-forum.com/t/date-in-a-textfield/186164/3 "2010-04-14T12:58:08Z")

</div>

On Apr 1, 10:49 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > %\> ![Calendar]()
> 
> > end
> 
> How are you showing the error message? An error here could be the  
> explanation for not seeing the message.
> 
> Have you tried using ruby-debug to break into your controller and see  
> what is happening when you submit? If you do not know how to do this  
> have a look at the rails guide on debugging athttp://guides.rubyonrails.org/.
> 
> Colin

Hi Team,

I am having a similar problem, also following along from railscast 32.

Any suggestions welcome. Is it an erb thing or something wrong with my  
model code? Also :formdate is a date field not a datetime field.

==========  
View throws this ERROR PAGE:  
ArgumentError in Contracts#new

Showing app/views/contracts/new.html.erb where line #84 raised:

wrong number of arguments (1 for 0)

Extracted source (around line #84):

81:   
82:

  
83:  
84: 
\<%= f.label :formdate, “Date” %\>  
\<%=  
f.text\_field :formdate\_string  
85: %\>
  
86: 
\<%= f.label :publication %\> \<%=  
f.text\_field :publication %\>
  
87: 
\<%= f.label :issue %\> \<%=  
f.text\_field :issue %\>

Request

Parameters:

None

Show session dump

* * *

Response

Headers:

# {“Content-Type”=\>“text/html”, “Cache-Control”=\>“no-cache”}

MODEL:  
class Contract \< ActiveRecord::Base

def formdate\_string  
formdate.to\_s(:db)  
end

def formdate\_string=(fdate\_str)  
self.formdate = Time.parse(fdate\_str)  
end

# end

VIEW: new.html.erb - extract

\<%= f.label :formdate, "Date" %\>  
 \<%= f.text\_field :formdate\_string %\> ==========

Thank you.

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 15, 2010, 3:34am UTC](https://www.ruby-forum.com/t/date-in-a-textfield/186164/4 "2010-04-15T03:34:39Z")

</div>

On Apr 14, 10:46 pm, Colin L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > model code? Also :formdate is a date field not a datetime field.
> 
> > Response  
> > def formdate\_string  
> > VIEW: new.html.erb - extract
> > 
> > \<%= f.label :formdate, "Date" %\>  
> > \<%= f.text\_field :formdate\_string %\>
> 
> That does not appear to be exactly the same code as shown in the error  
> above. Above it has a newline after :formdate\_string, as it shows the  
> %\> on line 85. Could that be part of the problem I wonder?
> 
> Colin

Thanks for your prompt reply. It wasn’t the new line, but that  
difference was well spotted.

I found it… makes sense now… ArgumentError… it wasn’t getting  
passed an argument for a new form. The form worked for the edit action  
when the field was populated.

I needed to initialize the formdate field with a value. So I added  
this to the “new” action of the controller.  
eg.

def new

```
@contract = Contract.new
@contract.formdate = Time.now.localtime.strftime("%Y-%m-%d")

```

end

Would you say this is good practice? or could I do it better elswhere  
or with different code?

Cheers.

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 14, 2010, 2:50pm UTC](https://www.ruby-forum.com/t/date-in-a-textfield/186164/5 "2010-04-14T14:50:33Z")

</div>

On 14 April 2010 03:47, Cote [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ArgumentError in Contracts#new  
> 84:
> 
> \<%= f.label :formdate, “Date” %\>  
> \<%=  
> f.text\_field :formdate\_string  
> 85: %\>
>   
> 86: 
> \<%= f.label :publication %\> \<%=  
> f.text\_field :publication %\>
>   
> 87: 
> \<%= f.label :issue %\> \<%=  
> f.text\_field :issue %\>

This is not a similar problem at all. The OP had an issue with  
validations, yours is showing an error.

I don’t see the problem immediately, but I suggest removing bits of  
code till it does not show the error, then putting it back a little at  
a time till you find exactly what is causing the problem.

> * * *
> 
> # class Contract \< ActiveRecord::Base
> 
> VIEW: new.html.erb - extract
> 
> \<%= f.label :formdate, "Date" %\>  
> \<%= f.text\_field :formdate\_string %\>

That does not appear to be exactly the same code as shown in the error  
above. Above it has a newline after :formdate\_string, as it shows the  
%\> on line 85. Could that be part of the problem I wonder?

Colin

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 16, 2010, 6:31am UTC](https://www.ruby-forum.com/t/date-in-a-textfield/186164/6 "2010-04-16T06:31:56Z")

</div>

Hi

> MODEL:  
> class Contract \< ActiveRecord::Base
> 
> def formdate\_string  
> formdate.to\_s(:db)  
> end
> 
> def formdate\_string=(fdate\_str)  
> self.formdate = Time.parse(fdate\_str)  
> end
> 
> end

```
 This happens when in the new does not get value And as one of the 

```

post in the railscast page suggest it can be solved with an instance  
varibale like

def formdate\_string  
@formdate\_string || (formdate.to\_s(:db) unless formdate.nil?)  
end

def formdate\_string=(fdate\_str)  
@formdate\_string = fdate\_str  
self.formdate = Date.parse(@formdate\_string)  
rescue ArgumentError  
@formdate\_invalid = true  
end

@formdate\_invalid can be used in def validate

Tom

---

<div class="post-metadata">

**Author:** ![tom-mac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/bc8723/32.png) [@tom-mac](https://www.ruby-forum.com/u/tom-mac)\
**Post date:** [April 16, 2010, 8:12am UTC](https://www.ruby-forum.com/t/date-in-a-textfield/186164/7 "2010-04-16T08:12:24Z")

</div>

Ok Cool, thank you.
