Forum: Ruby on Rails NoMethodError when saving checked data in checkbox

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
new rails user (Guest)
on 2007-02-22 05:46
Another question from me, a new person to ROR.

table a (has column id and name) and table b (has column id and name),
they have many_many relationship. so, joint table a_b introduced. a_b
only has a_id and b_id.

I am using


<% for b in @bs %>
  <%= check_box("b", b.id, (@a.bs.include?(b) ?
{:checked => 'checked'} : {:checked => false} )) %> <%= b.name %>
<% end %>


to show all available b for a. it works.

I am using

  def update
    @a = A.find(params[:id])

    b = B.find(params["b"].to_i)
    unless @a.bs.include?(b)
      @a.bs << b
    end

    if @a.update_attributes(params[:a])
      flash[:notice] = 'A was successfully updated.'
      redirect_to :action => 'show', :id => @a
    else
      render :action => 'edit'
    end
  end

to save all info into table a and joint table a_b

however, I got the following error message:

 NoMethodError in AController#update

undefined method `to_i' for {"1"=>"1", "2"=>"0", "3"=>"0",
"4"=>"1"}:HashWithIndifferentAccess

Any idea how to fix it? thanks.
This topic is locked and can not be replied to.