# Nesting forms or a way around it?

**URL:** <https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797>\
**Category:** Rails\
**Created:** [November 21, 2006, 7:30am UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797 "2006-11-21T07:30:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [November 21, 2006, 7:30am UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797/1 "2006-11-21T07:30:41Z")

</div>

So I know that you cannot nest forms in HTML as I’ve found out today,  
but I have a need to do this or get around it.

You can see the admin panel I’m building at:  
[http://www.juiceboxcommunications.com/admin/subscribers](http://www.juiceboxcommunications.com/admin/subscribers)

Click on the empty space in one of the table results to fly out  
complete subscriber data.  
In there, there’s a submit, I was trying to have a form for each  
subscriber which submits.

I also need a form around the checkboxes though, so I can handle the  
functions in the right panel such as select all, add selected to  
category, etc. etc.

The trouble is, that in my code involves nesting a form around the  
checkboxes (which involves nesting around the rest of the results). So  
I need a faux way to nest forms, or another workable solution if anyone  
has dealt with this.

the code is like this:

\<% for subscriber in @subscribers %\>  
\<%= check\_box(‘subscriber’, ‘checkBox’, :name =\> ‘subscriber\_ids[]’,  
:id =\> subscriber.id )%\>  
\<%= text\_field(‘subscriber’, ‘firstName’, :value =\>  
subscriber.firstName ) %\>  
\<%= text\_field(‘subscriber’, ‘lastName’, :value =\>  
subscriber.lastName ) %\>  
etc.  
etc.  
\<% end %\>

If anyone has experience with this, it is much appreciated.

Jason

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [November 21, 2006, 10:28am UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797/2 "2006-11-21T10:28:03Z")

</div>

Have a look at \*form\_remote\_tag \*that might help

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [November 21, 2006, 2:21pm UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797/3 "2006-11-21T14:21:03Z")

</div>

I’m not really a web developer so I might be missing some potential  
problems here, but from what I understand, there are many possible ways  
around the problem:  
a) Use one form around everything, but have the various Submit buttons  
rewrite the  
form’s “action” attribute to redirect the form to different actions or  
even controllers. Each controller/action then decides, what data from  
the form is relevant to that particular action and ignores the rest  
b) Use one form, but have the various Submit buttons set some parameters  
in a hidden field that your controller can see and decide what the  
intended action was  
c) Use multiple forms, but make them not nested in html. You could use  
some css positioning (and/or javascript if necessary) to make them  
visually appear in the right places

Cheers,  
Mati

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [November 21, 2006, 3:39pm UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797/4 "2006-11-21T15:39:22Z")

</div>

Mati wrote:

> intended action was  
> c) Use multiple forms, but make them not nested in html. You could use  
> some css positioning (and/or javascript if necessary) to make them  
> visually appear in the right places

Yes.

Another way is to dispose of either the outer form  
or inner set of forms and instead submit that data  
using submit\_to\_remote and its :submit parameter.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [November 21, 2006, 3:48pm UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797/5 "2006-11-21T15:48:32Z")

</div>

Mati wrote:

> c) Use multiple forms, but make them not nested in html. You could use  
> some css positioning (and/or javascript if necessary) to make them  
> visually appear in the right places

I would second this. Might be a pain to hack, but I’m not aware of any  
common way to trick the browser into nesting forms.

Although I think you can add form elements without declaring an actual  
form. You could use javascript to seralize the values into a string, and  
pass it via AJAX, bypassing any kind of form action. (Or if for some  
reason, you can’t nest form elements, create a custom graphic checkbox,  
and use an onClick event to register the true/false value and pass  
through AJAX).

None of these are a particularly pretty solution. I tend to subscribe to  
the philsophy that if my design attempts to do something that isn’t  
generally supported or commonly done, it’s probably time to rethink my  
design. Brute force hacking my way through an HTML/CSS/Javascript  
problem often results in lots of frustration, and a final product that  
doesn’t always feel right.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [November 21, 2006, 6:26pm UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797/6 "2006-11-21T18:26:29Z")

</div>

The solution I am working on right now is duplicating the checkboxes  
elsewhere on the page in a form that will be hidden. Then, I use  
javascript onSubmit to pass the values for ‘checked’ to the hidden form  
checkboxes. It is kind of hacky, but so far it looks like it might work

- and then I can just submit the duplicate values in a second form not  
seen by the user. Any comments from people who’ve tried this before are  
appreciated. The interface is based heavily on javascript so I’m not  
worried about people that don’t have it turned on, they can’t use the  
system anyway.

The trouble I was having in looking at the remote submit functions, is  
that I don’t understand how to pass it only the values that I want to  
update on my controller.

I have a controller that is:

def update\_subscriber {  
@subscriber = Subscriber.find(params[:id])  
If @subscriber.update\_attributes

# do the action here

end  
}

whereas if I have it submit the whole form I’m not a good enough web  
developer yet to sort through all that data. Or am I making it more  
complicated than it needs to be? I’ve been known to do that to myself  
from time to time…

Thx,

J

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [November 21, 2006, 7:30pm UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797/7 "2006-11-21T19:30:26Z")

</div>

The old (and backwards compatible) approach is to use one form.

When you receive the parameters, evaluate the value of the submit button  
that was pressed (it’s value will be the button your user pressed.)  
Based on  
this, fire the appropriate method.

---

<div class="post-metadata">

**Author:** ![jpfeifer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/65b543/32.png) [@jpfeifer](https://www.ruby-forum.com/u/jpfeifer)\
**Post date:** [November 21, 2006, 6:48pm UTC](https://www.ruby-forum.com/t/nesting-forms-or-a-way-around-it/76797/8 "2006-11-21T18:48:16Z")

</div>

YIKES!!!

this would fall flat without JavaScript enabled!

with more and more users on the mobile web, backwards compatibility is  
more important than ever.

On 11/21/06, Mati [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> in a hidden field that your controller can see and decide what the
> 
> >

–

Chris M.  
Web D.  
Open Source & Web Standards Advocate

> **[Chriscodes.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=chriscodes.com)**
>
> Painless, quick delivery of your domain name. Fast and professional service.
