# Checkbox\_tag outside a form?

**URL:** <https://www.ruby-forum.com/t/checkbox-tag-outside-a-form/95943>\
**Category:** Rails\
**Created:** [June 20, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/checkbox-tag-outside-a-form/95943 "2006-06-20T19:34:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [June 20, 2006, 7:34pm UTC](https://www.ruby-forum.com/t/checkbox-tag-outside-a-form/95943/1 "2006-06-20T19:34:58Z")

</div>

Is it possible to use checkbox\_tag outside a form? I’ve put it in a  
view and it renders but I get nothing back in the params hash. Is there  
a way to get the value? Like maybe with some JS onsubmit to populate  
the params hash myself?

Thanks,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 3, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/checkbox-tag-outside-a-form/95943/2 "2006-08-03T14:24:04Z")

</div>

Bill W. wrote:

> Is it possible to use checkbox\_tag outside a form? I’ve put it in a view and it renders but I get nothing back in the params hash. Is there a way to get the value? Like maybe with some JS onsubmit to populate the params hash myself?
> 
> Thanks,  
> Bill

I think the easiest solution is to spread our your tags so  
that the checkbox falls between them. If there’s just one form on the  
page, I don’t think there’s anything particularly wrong with

…

although that’s usually a bit extreme.

If, for whatever reason, that’s not practical (maybe due to different  
partials and layouts breaking things up too much), then you could place  
a hidden tag in the form, and use an onChange event at the checkbox to  
update the value of the hidden tag. Then, the value of the hidden tag is  
what will be available in the params hash on the server. That’s a bit  
more complicated, but not too difficult if you’re familiar with  
javascript and onChange events. Of course, if the user has javascript  
disabled, then this won’t work at all. So, I’d recommend just finding a  
way to spread out those tags.

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [August 15, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/checkbox-tag-outside-a-form/95943/3 "2006-08-15T14:21:42Z")

</div>

Hi Jonathan,

Jonathan M. wrote:

> I think the easiest solution is to spread our your
> 
> tags so that the checkbox falls between them. If there's just one form on the page,

That’s the problem. This posting was actually a follow-on to an earlier  
one  
where I thought the solution to my problem could be solved by using  
checkbox\_tag outside a form. Doesn’t look like it now. I’ve included  
the  
earlier post below just in case you have any ideas.

Thanks much,  
Bill

* * *

----- Original Message -----  
From: Bill W.  
To: removed\_email\_address@domain.invalid  
Sent: Tuesday, June 20, 2006 9:03 AM  
Subject: [Rails] Soliciting suggestions - Form within a Form

Greetings!

I could use some suggestions on how to proceed from where I am to  
where I  
want to go.

I’m working on an app that collects info from the visitor and saves it  
to  
a file. The file can later be read in for update. To collect the info,  
the  
app presents a form with a set of checkboxes. The state of the  
checkboxes  
at initial presentation is controlled by a summary record that is  
created /  
populated when the file is read in for updating. The record contains  
fields  
for each of the checkboxes with the value = ‘y’ or ‘n’. In addition to  
a  
set of ‘standard’ selections, the form also has a checkbox labeled  
‘Other’.

Currently, when the form is submitted, individual records are created  
for  
each checked checkbox for the standard selections. If the ‘Other’  
checkbox  
is checked, after records are created for the ‘standard’ items, I render  
a  
second page with a form that allows them to enter one or more other  
items.  
Individual records for those items are created via RJS as the items are  
entered.

I’d like to allow the addition of the ‘Other’ items on the same page  
as  
the standard checkbox selections. If the visitor checks the ‘Other’  
checkbox, I want to replace the checkbox with the partial that’s  
currently  
being rendered on the second page. I can do that by putting the ‘Other’  
checkbox in a

 and doing an RJS ‘replace\_html’.

Unfortunately RoR doesn’t support forms within forms. Clicking the  
‘Submit’ button on the sub-form submits the entire form. So that  
doesn’t  
work for me.

I’m considering eliminating the top level form and just using a  
‘button\_to’ to pass the entered info from the view back to the  
controller.  
Pro: The ‘Other’ form processing works fine  
Con: Since they’re not contained within a form anymore, the checkboxes  
aren’t serialized and passed to the controller via the params hash.

I believe I can use ‘checkbox\_tag’ instead of ‘checkbox’ to get the  
values  
back to the controller, but that leaves me with the chore of  
initializing  
them for presentation from the values in the summary record. Overall,  
this  
path feels non-Railish.

Any suggestions would be greatly appreciated.

Thanks,  
Bill

---

<div class="post-metadata">

**Author:** ![wpwood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d6d6ee/32.png) [@wpwood](https://www.ruby-forum.com/u/wpwood)\
**Post date:** [September 4, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/checkbox-tag-outside-a-form/95943/4 "2006-09-04T14:45:54Z")

</div>

hi there,  
it´s not really that ror doesn´t support forms in forms… it´s the  
browsers that don´t.  
we had a problem a bit like yours.  
what we did is just place the “subform” in a

 that was outside the  
other form and just positioned it “inside” the other form later.  
that worked.  
kind regards,  
alexander
