# Check\_box and that automatic hidden\_field messes up my form

**URL:** <https://www.ruby-forum.com/t/check-box-and-that-automatic-hidden-field-messes-up-my-form/144978>\
**Category:** Rails\
**Created:** [August 4, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/check-box-and-that-automatic-hidden-field-messes-up-my-form/144978 "2008-08-04T22:18:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [August 4, 2008, 10:18pm UTC](https://www.ruby-forum.com/t/check-box-and-that-automatic-hidden-field-messes-up-my-form/144978/1 "2008-08-04T22:18:13Z")

</div>

I’m having a form where the user can select the line items that he wants  
to create. All line items are new and don’t have an id.  
I realized that check\_box created a hidden field that’s supposed to help  
me, but it doesn’t work when I receive the params[:line\_items]. I get a  
double number of line items. I can fix it in the controller, but it’s  
just ugly. Advices?

\<%fields\_for ‘line\_items[]’, li do |l|%\>  
\<%=l.check\_box :selected\_for\_save%\> \<%=l.hidden\_field  
:container\_id%\>  
\<%=l.hidden\_field :amount%\>  
\<%end%\>

line\_items:

- !map:HashWithIndifferentAccess  
selected\_for\_save: “1”
- !map:HashWithIndifferentAccess  
container\_id: “1901”  
selected\_for\_save: “0”  
amount: “490.0”
- !map:HashWithIndifferentAccess  
selected\_for\_save: “1”
- !map:HashWithIndifferentAccess  
container\_id: “1912”  
selected\_for\_save: “0”  
amount: “465.0”
- !map:HashWithIndifferentAccess  
selected\_for\_save: “1”
- !map:HashWithIndifferentAccess  
container\_id: “1913”  
selected\_for\_save: “0”  
amount: “465.0”

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [August 4, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/check-box-and-that-automatic-hidden-field-messes-up-my-form/144978/2 "2008-08-04T22:42:00Z")

</div>

Frederick C. wrote:

> On 4 Aug 2008, at 21:18, Constantin G.  
> \<removed\_email\_address@domain.invalid
> 
> > wrote:
> 
> > 
> 
> Use check\_box\_tag instead of check\_box ?
> 
> Fred

This is what I’m using:

\<%=check\_box\_tag ‘line\_items[][selected\_for\_save]’ %\>  
instead of  
\<%=l.check\_box :selected\_for\_save%\>

I find it ugly. I don’t want to mix formhelpers with formtaghelpers.

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [August 4, 2008, 10:24pm UTC](https://www.ruby-forum.com/t/check-box-and-that-automatic-hidden-field-messes-up-my-form/144978/3 "2008-08-04T22:24:13Z")

</div>

On 4 Aug 2008, at 21:18, Constantin G.  
\<removed\_email\_address@domain.invalid

> wrote:

> 

Use check\_box\_tag instead of check\_box ?

Fred

---

<div class="post-metadata">

**Author:** ![costi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/4491bb/32.png) [@costi](https://www.ruby-forum.com/u/costi)\
**Post date:** [August 4, 2008, 11:02pm UTC](https://www.ruby-forum.com/t/check-box-and-that-automatic-hidden-field-messes-up-my-form/144978/4 "2008-08-04T23:02:56Z")

</div>

On Aug 4, 9:42 pm, Constantin G. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> \<%=check\_box\_tag ‘line\_items[][selected\_for\_save]’ %\>  
> instead of  
> \<%=l.check\_box :selected\_for\_save%\>
> 
> I find it ugly. I don’t want to mix formhelpers with formtaghelpers.

It’s kludgy but right now it’s the only way if line\_items is to be an  
array parameter. The way that the form parameter parser detects that a  
new array element is starting is when it spots a repeated parameter  
name.  
If you make line\_items a hash parameter you won’t have this problem,  
ie generate inputs

line\_items[1][selected\_for\_save]  
line\_items[2][selected\_for\_save]

(sequential ids 1,2 etc… will do fine, but if you have some more  
natural key than you can use that too.)

Fred
