Check_box and that automatic hidden_field messes up my form

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”

Frederick C. wrote:

On 4 Aug 2008, at 21:18, Constantin G.
<[email protected]

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.

On 4 Aug 2008, at 21:18, Constantin G.
<[email protected]

wrote:

Use check_box_tag instead of check_box ?

Fred

On Aug 4, 9:42 pm, Constantin G. <rails-mailing-l…@andreas-
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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs