Hallo Ich versuche, ein Multi-Select zu erstellen, um eine Viele-zu-viele Join-Tabelle aufzufüllen. Ich bin in der Lage, die neue newform zu verketten, bekomme aber "AssociationTypeMismatch", wenn ich versuche, meinen Datensatz zu speichern. Die Lösungen, die ich im Internet finde, lösen mein Problem nicht.
der Hoffnung, jemand kann lösen, was soll ich loswerden tun von "AssociationTypeMismatch"ActiveRecord :: AssociationTypeMismatch auf vielen zu vielen Relaitonship
class Presenter < ActiveRecord::Base
belongs_to :seminar
belongs_to :person
end
class Seminar < ActiveRecord::Base
attr_accessible :description, :title,
has_many :presenters, :foreign_key => "person_id"
has_many :lecturer, :through => :presenters, :source => :person
accepts_nested_attributes_for :lecturer, :presenters
end
class Person < ActiveRecord::Base
attr_accessible :first_name, :last_name
has_many :presentors
has_many :lecturingAt, :through => :presentors
def fullName
first_name + " " + last_name
end
end
seminars_controller.rb
def new
@seminar = Seminar.new
@current_presenters = Person.find(:all)
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @seminar }
end
end
....
def create
@seminar = Seminar.new(params[:seminar])
respond_to do |format|
if @seminar.save
format.html { redirect_to(@seminar, :notice => 'Seminar was successfully created.') }
format.xml { render :xml => @seminar, :status => :created, :location => @seminar }
else
format.html { render :action => "new" }
format.xml { render :xml => @seminar.errors, :status => :unprocessable_entity }
end
end
end
Seminare/_form.html.erb. hat, was meine Sammlung mit den Namen und Persion-IDs von möglichen leucers füllt.
....
<div class="field">
<%= f.label :presenter_id %><br />
<%= collection_select(:seminar,:lecturer,@current_presenters, :id, :fullName,{}, {:multiple=>true}) %>
....
auf der params in mein Controller übergeben Absenden
Parameters: {...., "seminar"=>{ "lecturer"=>["1", "2"], "title"=>"1234567890", "description"=>"ASDFGHJKL:"}, "commit"=>"Create Seminar"}
Erste Fehler:
ActiveRecord::AssociationTypeMismatch (Instructor(#86075540) expected, got String(#73495120)):.
Did das versuchst du mit formtastic. Es hat sehr gute Optionen, um eine Viele-zu-viele-Beziehung mit Checkboxen anzuzeigen. https://github.com/justinfrench/formtastic – Sairam