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

logo Created with Sketch.

Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

© 2021 GoRails, LLC. All rights reserved.