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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060620/f31f5417/attachment.html
Bill Walton 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, > BillI think the easiest solution is to spread our your <form></form> 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 <body ...><form ...> ... </form></body></html> 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 <form/> tags.
Hi Jonathan, Jonathan Motta wrote:> I think the easiest solution is to spread our your > <form></form> 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 Walton To: rails@lists.rubyonrails.org 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 <div> 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
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 <div> that was outside the other form and just positioned it "inside" the other form later. that worked. kind regards, alexander Bill Walton wrote:> Hi Jonathan, > > Jonathan Motta wrote: > >> I think the easiest solution is to spread our your >> <form></form> 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 Walton > To: rails@lists.rubyonrails.org > 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 <div> 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Hi Alexander, alexander wrote:> it?s not really that ror doesn?t support forms in forms.. it?s the > browsers that don?t.I need to understand more about the browser-side issue with this. Is it that they don''t support behavior that''s allowed / specified by W3C? Or does the spec itself say this is inappropriate?> we had a problem a bit like yours. what we did is just place the > "subform" in a <div> that was outside the other form and just positioned > it "inside" the other form later. that worked.The more I''ve thought about it, the more this seem like the ''right'' way to approach the problem. Make the ''containing'' form more of a visual device with respect to sub-form elements. Unfortunately, it seems to require considerable expertise in CSS in order to grow / shrink the containing form dynamically based on the size of the sub-forms. Or maybe there''s an easier way to handle the positioning. If anyone has any knowledge to share on this, I''d definitely appreciate it. My current level of CSS skill is very basic. At any rate... The solution I came up with is to 1) eliminate the containing form, using button_to to advance to the next page 2) put each checkbox (still bound to the model) inside a <div> with an accompanying observe_field that 3) causes RJS-based update of the summary record and create / delete activity for the related individual records. The use of the checkbox, rather than checkbox_tag allows initial population of the checkboxes based on the values in the summary record. Eliminating the containing form meand
Fat-fingered Oops ;-) finishing for benefit of future users... Hi Alexander, alexander wrote:> it?s not really that ror doesn?t support forms in forms.. it?s the > browsers that don?t.I need to understand more about the browser-side issue with this. Is it that they don''t support behavior that''s allowed / specified by W3C? Or does the spec itself say this is inappropriate?> we had a problem a bit like yours. what we did is just place the > "subform" in a <div> that was outside the other form and just positioned > it "inside" the other form later. that worked.The more I''ve thought about it, the more this seem like the ''right'' way to approach the problem. Make the ''containing'' form more of a visual device with respect to sub-form elements. Unfortunately, it seems to require considerable expertise in CSS in order to grow / shrink the containing form dynamically based on the size of the sub-forms. Or maybe there''s an easier way to handle the positioning. If anyone has any knowledge to share on this, I''d definitely appreciate it. My current level of CSS skill is very basic. At any rate... The solution I came up with is to 1) eliminate the containing form, using button_to to advance to the next page 2) put each checkbox (still bound to the model) inside a <div> with an accompanying observe_field that 3) causes RJS-based update of the summary record and create / delete activity for the related individual records. The use of the checkbox, rather than checkbox_tag, accomplishes the initial population of the checkboxes based on the values in the summary record. Eliminating the containing form means that the values of the checkboxes won''t be submitted to the controller via the params hash, but the RJS-based processing make that a non-issue. Best regards, Bill
<...>> > it?s not really that ror doesn?t support forms in forms.. it?s the > > browsers that don?t.<...>> I need to understand more about the browser-side issue with this. Is it > that they don''t support behavior that''s allowed / specified by W3C? Or does > the spec itself say this is inappropriate?Where did you get the idea, that nested forms are allowed? From DTDs: HTML 4.01: <!ELEMENT FORM - - (%block;|SCRIPT)+ -(FORM) -- interactive form --> XHTML 1.0 <!ELEMENT form %form.content;> <!-- forms shouldn''t be nested --> Just for the sake of completeness: HTML 3.2: <!ELEMENT FORM - - %body.content -(FORM)> HTML 2.0 <!ELEMENT FORM - - %body.content -(FORM) +(INPUT|SELECT|TEXTAREA)> That this means is: nesting forms is not allowed and never was. Regards, Rimantas -- http://rimantas.com/