0

How can I make 10 or more than 10 check box by a for loop

This is my code

In view.html.erb

<% (0...100) do |i| %>
  <%= f.check_box :chkbox_ary[i], {:checked=>false, :style => "width: 20px; height: 20px;"} %>
  <%= f.label "checkbox" %>
<% end %>

And in model

attr_accessor :chkbox_ary

def initialize(attributes = {})
  @chkbox_ary = []
end

I think this is OK, but I always get error

Internal Server Error
expected Array (got Rack::Utils::KeySpaceConstrainedParams) for param `travel'

How can I do to get my purpose or have some reference for me? Thanks

1 Answer 1

1

Your loop is missing an iterator:

(0...100).each do |i|

And you cannot use an index on a symbol - use the name of the array, and interpolate the index variable:

f.check_box "chkbox_ary[#{i}]" #...
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your reply and correction. But how can I communicate with my attr_accessor in model. "chkbox_ary[#{i}]" seem not means :chkbox_ary Thanks
Your checkbox array is not part of an object, so you can't access it using the object referenced by your form helper f. Use this version instead, passing the tag name and value separately: check_box_tag "chkbox_ary[#{i}]", @ckbox_ary[i], ...
I'm so sorry, I still can't solve my problem. When I use :ckbox_ary[0], it will show 'c'. This is website I make. taxifong.com/travel/travel. I only want to use a attr_accessor to record checkbox value, and I will use notifier to send the mail. <% if @sender.chkbox_ary[0] == "1" %> <%= test %> <BR><% end %> My previous method is use many attr_accessor, but I think this is not good

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.