Laurent Passebecq

Joined

50 Experience
0 Lessons Completed
0 Questions Solved

Activity

I have a form with a selectbox which allow to select differents values. When I want to save the form I have an exception :

ActiveRecord::AssociationTypeMismatch at xxxxxxxxxxxxxxxxxxxxx
Site(#70058148983820) expected, got String(#8388700)

Here is the form which generate the select box

col-md-9
.input-group.btn-group
%span.input-group-addon
=t('activerecord.attributes.user.volunteer_wishes_sites')
= f.collection_select :wishes_sites, @sites, :id, :name, {:selected => @user.wishes_sites.map(&:id)}, {:multiple => true, :class=>"sites_multiselect"}

In my controller I have

    def user_params
      params.require(:user).permit(
        :wishes_sites =>[],
      )
    end

The model

    class User < ActiveRecord::Base
      ....
      has_and_belongs_to_many :wishes_sites, :class_name => "Site", :join_table => :wishes_sites
    end

here is the line which produces the bug
if @user.update_attributes(user_params)

Here are the migration

    create_table :sites, force: true do |t|
      t.string   "name"
      t.datetime "created_at"
      t.datetime "updated_at"
      t.text     "address"
      t.text     "description"
    end

    create_table :wishes_sites, id: false, force: true do |t|
      t.integer "user_id"
      t.integer "site_id"
    end

Thanx for your help

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

© 2024 GoRails, LLC. All rights reserved.