Rails checkbox and params

10,557

You'll already get only the ones that were checked; unchecked boxes aren't sent to the server.

Share:
10,557
Arthur Wilson
Author by

Arthur Wilson

Updated on June 04, 2022

Comments

  • Arthur Wilson
    Arthur Wilson about 2 years

    I am using many checkboxes in a page as the following:

    <%= check_box_tag(:one, value = 1) %></p>
    

    Imagine that it goes from 1 to 20, all the same with the value changed. How can I get in the controller the params of the ones that were checked?

    Example: If user checks 3, 5 and 10 how can I get just those params in a single params?

  • Arthur Wilson
    Arthur Wilson over 12 years
    But how can I get them from just a single params? Just does not seems right to get it using a lot of params.
  • Dave Newton
    Dave Newton over 12 years
    @ArthurWilson Not sure what you mean. If they're all named the same thing don't they come as an array? (I don't recall off the top of my head, sorry.)
  • Arthur Wilson
    Arthur Wilson over 12 years
    Like if I name them :numbers[] and call params(:numbers) it will work?
  • Dave Newton
    Dave Newton over 12 years
    @ArthurWilson The name shouldn't include the colon, but yes, I think so--why not just try it?
  • Arthur Wilson
    Arthur Wilson over 12 years
    Actually I did 'numbers[]' and got the params as params[:numbers]. Thanks for the help.