mercredi 5 décembre 2018

Rails form select for join table produces ActiveRecord::AssociationTypeMismatch

I am trying to setup the data input for a database which contains for example projects. Projects can be part of a series, have multiple curators, etc.

I either get the series form displayed with:

<%= f.collection_select :series, Series.order('word asc'), :id, :word, {}, {:class => 'select-style'} %>

with proper styling, but upon submit I get

ActiveRecord::AssociationTypeMismatch in Admin::ProjectsController#update
Series(#2212122800) expected, got String(#2183812080)

with

<%= f.select :series, Series.all.collect{ |p| [ p.word, p.id ]}, :include_blank => true, :class => "select-style" %>

CSS is not applied, but I manage to get a blank option. Submit produces the same response.

For the curators

<%= f.select :curators, options_for_select(Author.all.map{|c| [c.name, c.id]}, f.object.curators), {}, {:class => "form-control selectpicker", :include_blank => true, :multiple => true} %>

produces a multi select field with no styling and on submit I get a similar error

ActiveRecord::AssociationTypeMismatch in Admin::ProjectsController#update
Author(#2214244880) expected, got String(#2183812080)

I need to be able to apply my own CSS to the selector, have the ability for single, multiple and blank. The forms do not work and no version permits all options.

These relations are established through join-tables. My models are:

Project Model

class Project < ActiveRecord::Base
  attr_accessible :series, :curators

# Associations 
  has_and_belongs_to_many :curators, :class_name => "Author", :join_table => "projects_curators"
  has_and_belongs_to_many :series, :join_table => "projects_series" 
end

Series Model

class Series < ActiveRecord::Base
  attr_accessible :word

# Associations
  has_and_belongs_to_many :projects, :join_table => "projects_series" 

end

Curator join table

class ProjectsCurator < ActiveRecord::Base  
  attr_accessible :project_id, :author_id

# Associations
  belongs_to :project
  belongs_to :author
end

Author model

class Author < ActiveRecord::Base
  attr_accessible :name

# Associations
  has_and_belongs_to_many :projects, :join_table => "projects_curators"
end

Aucun commentaire:

Enregistrer un commentaire