Creating new record from a form? (stringify_keys! error)

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.

@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.

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?

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

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

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 (
http://www.ruby-forum.com/topic/76340#119128 ) for that
as it might help others to split up the issue.

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”}}

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs