Forum: Ruby on Rails checkboxes

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Eec43f946d65be32e6b9fd2b3a1582a5?d=identicon&s=25 Adam Groves (Guest)
on 2005-12-14 17:57
(Received via mailing list)

I've got a form like so:

<% @companies.each do |company| %>
<%= %>
<input type="checkbox" name="companies[]" value="<%= %>"
<% end %>

which posts eg. this hash:
 *Parameters*: {"commit"=>"Dispatch", "companies"=>["36", "29", "31",

to this action:

def dispatch
  if params[:companies]
    params[:companies].each do |key, value|
      @company = Company.find(value)
      @companies << @company

I get the error message "Couldn't find Company without an ID"


Thanks in advance

8217faf2bfdfa7daf10135d41ddd421e?d=identicon&s=25 Jeff Cohen (jeff)
on 2005-12-14 18:10
I think params[:companies] is an Array, not a Hash, so .each won't pass
you the key, just the value.  So you need something like:

params[:companies].each do |value|

Hope this helps

This topic is locked and can not be replied to.