Forum: Ruby on Rails Can I force a form to submit empty fields?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-30 23:49
I've seen some Java implementations but is there a simpler way to force
a form to submit ALL fields even if some are empty?  I need a consistent
number of responses to tabulate the results, so having NULL or empty
strings doesn't bother me.  The programmatic challenges of trying to
reconstruct survey results outweigh the benefits of reducing a few
database NULLs.  Thanks in advance!
4c438a80fc30661ad619ea177cf9cbd0?d=identicon&s=25 Freddy Andersen (Guest)
on 2009-01-30 23:57
(Received via mailing list)
In Rails if the form field is empty it will submit an empty params
value.. Thats how it works by default.

Maybe an example would be better?


If you had two inputs, i1 and i2 and i1 had the word here and i2 was
empty when you press submit you get this back to you controller:

i1="here" i2=""

and if there is no validation rules in the model that would be saved
with the blank field..

Sounds like you are thinking Java and not Rails...

On Jan 30, 2:49 pm, Taylor Strait <rails-mailing-l...@andreas-s.net>
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-31 00:19
Freddy Andersen wrote:
> In Rails if the form field is empty it will submit an empty params
> value.. Thats how it works by default.
>
> Maybe an example would be better?
>
>
> If you had two inputs, i1 and i2 and i1 had the word here and i2 was
> empty when you press submit you get this back to you controller:
>
> i1="here" i2=""
>
> and if there is no validation rules in the model that would be saved
> with the blank field..
>
> Sounds like you are thinking Java and not Rails...
>
> On Jan 30, 2:49�pm, Taylor Strait <rails-mailing-l...@andreas-s.net>

Thanks for the response.  My form is not tied to a model and is
generated dynamically.  But I have my submission button redirect to a
view that displays the params - many fields that are skipped are NOT
submitted as params.  I see now that is DOES submit empty text fields.
But it does not submit empty radio button groups or checkboxes.  I need
tall form elements as params even if they aren't checked / selected /
filled out.  Since the forms are generated dynamically I cannot manually
add empty fields on save.  So I guess my refined question is:

How do I force the submission of radio buttons / checkboxes if empty?
8e7e5b934fcbcdfb475072685755457f?d=identicon&s=25 Taylor Strait (taylorstrait)
on 2009-01-31 01:08
I solved this issue with hidden fields.  You must put a hidden field
with the SAME NAME with the value = '0' (or whatever you want to mean
unchecked).  This sets a default of '0' which is overwritten by the
checkbox.
However, the hidden field MUST be after the checkbox for it to work,
which is counter-intuitive.  If the hidden field is before it will
overwrite the real value, making all values '0.'
4c438a80fc30661ad619ea177cf9cbd0?d=identicon&s=25 Freddy Andersen (Guest)
on 2009-01-31 17:47
(Received via mailing list)
It was funny because I was thinking checkbox is different when I wrote
my reply but left it out anyways...

You have to specify what the default is and that should get you 1 or 0
or yes or no...

check_box("product", "published", {}, '1', '0')
or
check_box("puppy", "gooddog", {}, "yes", "no")

Btw the check_box form helper will great a hidden field anyway...
This topic is locked and can not be replied to.