# Creating new record from a form? (stringify\_keys! error)

**URL:** <https://www.ruby-forum.com/t/creating-new-record-from-a-form-stringify-keys-error/101431>\
**Category:** Rails\
**Created:** [August 4, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/creating-new-record-from-a-form-stringify-keys-error/101431 "2006-08-04T20:45:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 4, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/creating-new-record-from-a-form-stringify-keys-error/101431/1 "2006-08-04T20:45:51Z")

</div>

Appologies if this has been covered, But it seems the search is busted.

I have a simple model object “Biostat” which I am trying to add a new  
record to.

this is the form:

\<%= form\_remote\_tag(  
:update=\>‘statsTable’,  
:url=\>{:action=\>‘new’},  
:html=\> {:action=\>‘new’, :method=\>‘post’},  
:complete =\> “new Effect.BlindUp(‘admin-stats-panel’, {duration:  
.25})”)%\>  
Date: \<%= text\_field\_tag ‘the\_date’, @today, :size=\>12 %\>  
Height (in): \<%= text\_field\_tag ‘height’, ‘’, :size=\>5 %\>  
Weight (lbs): \<%= text\_field\_tag ‘weight’, ‘’, :size=\>5 %\>  
\<%= submit\_tag ‘Save’ %\>  
\<%=end\_form\_tag%\>

and this is the “new” method:

def new  
@today = Date.today

```
if request.post? && (params[:the_date])
  @stat = Biostat.new(params[:the_date])
  @stat.weight = params[:weight]
  @stat.height = params[:height]

  if @stat.create
    flash[:notice] = "successfully saved"
  else
    flash[:notice] = "FAILED"
  end
else
  render_partial 'newStatInfo'
end

```

end

When run I get " undefined method `stringify\_keys!’ for “08 / 04 /  
2006”:String "

I’m pretty sure I’m not creating the record properly but I thought is  
was as simple as “Biostat.new”  
the only condition is that ‘data’ cannot be null.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 5, 2006, 1:37am UTC](https://www.ruby-forum.com/t/creating-new-record-from-a-form-stringify-keys-error/101431/2 "2006-08-05T01:37:08Z")

</div>

> @stat = Biostat.new(params[:the\_date])

You want:

@stat = BioStat.new(:date =\> params[:the\_date])

the “new” method expects a hash, with the name of the columns as the  
key, and the value as the hash value.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 5, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/creating-new-record-from-a-form-stringify-keys-error/101431/3 "2006-08-05T19:40:25Z")

</div>

Okay, so if I change the mysql field to ‘varchar’ instead of ‘date’ then  
I don’t get any error. So what do I have to do special for inserting  
into a ‘date’ field?

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 6, 2006, 10:08am UTC](https://www.ruby-forum.com/t/creating-new-record-from-a-form-stringify-keys-error/101431/4 "2006-08-06T10:08:03Z")

</div>

Todd S. wrote:

> Okay, so if I change the mysql field to ‘varchar’ instead of ‘date’ then  
> I don’t get any error. So what do I have to do special for inserting  
> into a ‘date’ field?

In your form use one of the helper methods from DateHelper to get user  
input like:  
[http://api.rubyonrails.com/classes/ActionView/Helpers/DateHelper.html#M000469](http://api.rubyonrails.com/classes/ActionView/Helpers/DateHelper.html#M000469)

When you provide user with text field to enter date you also have to  
parse this date manually to Time class instance. Then you can assign  
created Time instance to model field.

Providing user with text field for entering dates is rarely a good idea,  
unless you have written parser that deals with dates like ‘tomorrow’ or  
‘next year’. That kind of input happens quite often. And when you try to  
force user to use some specific format (like yyyy/mm/dd) you usually get  
a very bad user expirience.

Cheers,  
Bragi

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 8, 2006, 3:34am UTC](https://www.ruby-forum.com/t/creating-new-record-from-a-form-stringify-keys-error/101431/5 "2006-08-08T03:34:53Z")

</div>

Good advice. Thank you.

In the transition I find other problems with actually getting the date  
to be inserted correctly.

I’ve started another thread here (  
[Mysql Date field not getting set correctly - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/76340#119128) ) for that  
as it might help others to split up the issue.

---

<div class="post-metadata">

**Author:** ![sporkyy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c89c15/32.png) [@sporkyy](https://www.ruby-forum.com/u/sporkyy)\
**Post date:** [August 5, 2006, 2:56am UTC](https://www.ruby-forum.com/t/creating-new-record-from-a-form-stringify-keys-error/101431/6 "2006-08-05T02:56:41Z")

</div>

Alex W. wrote:

> > @stat = Biostat.new(params[:the\_date])
> 
> You want:
> 
> @stat = BioStat.new(:date =\> params[:the\_date])
> 
> the “new” method expects a hash, with the name of the columns as the  
> key, and the value as the hash value.

This, unfortunately, yields:

```
ActiveRecord::StatementInvalid (Mysql::Error: Column 'date' cannot 

```

be null: INSERT INTO biostats (`weight`, `date`, `height`) VALUES(0.0,  
NULL, 0.0)):

and if I try:  
@stat = Biostat.new(:date =\> params[:date], :weight =\>  
params[:weight], :height =\> params[:height])

I get:

```
ActiveRecord::StatementInvalid (Mysql::Error: Column 'date' cannot 

```

be null: INSERT INTO biostats (`weight`, `date`, `height`) VALUES(NULL,  
NULL, NULL)):

even though in the log I see:  
Parameters: {“commit”=\>“Save”, “date”=\>“2006-08-04”,  
“action”=\>“new”, “controller”=\>“stats”, “stat”=\>{“weight”=\>“12”,  
“height”=\>“34”}}
