Topic: Forms without a supporting model

Hi all,

Few problems connected to the same theme.

First, how do i make a form with a input field which is not associated with anything in the model. I need to have a checkbox which signifies not to insert / update the date field on the table but to insert / update to null instead

i've been trying to no avail

CONTROLLER:
if @record.date_obtained.nil?
  params[:set_null] = "1"
else
  params[:set_null] = "0"
end

VIEW:
check_box :set_null, @params[:set_null]

but it doesn't check the checkbox when it should - any ideas?

Second, when i have a record already in the DB and update it, I need to be able to change the date value back to null if the checkbox has been ticked. And i want to do it before calling update_attributes but

params[:record][:date_obtained] = nil
if @record.update_attributes(params[:record])
...

won't work - any help here appreciated greatly too

Thanks
Garrett

Re: Forms without a supporting model

There are two kinds of form helpers, those that end with "_tag" and those that don't. The one's that do end in '_tag' are generic form helpers not associated with a model. See the documentation the list of helpers. check_box_tag should do what you want.

As for your second problem, I would think that code would work. I believe the params hash is frozen, but I thought sub-hashes could be changed. What is happening? Is the date just not getting set to null? Try outputting the value of params[:record][:date_obtained] right before saving to make sure it's getting set.

Railscasts - Free Ruby on Rails Screencasts

Re: Forms without a supporting model

Ryan

it doesn't change the value of the sub-hash and the update just runs in with the value returned from the form regardless.

Depressing!

as with the _tag, i'll look into it,

Cheers
Garrett

Re: Forms without a supporting model

On checking, the hash is set to nil but the update_attributes pays no attention and updates with the original date if that makes any sense.