您好我正在尝试构建一个多选以填充多对多连接表。我可以创建新的newform但是当我尝试保存我的记录时会得到“AssociationTypeMismatch”。 我在网上找到的解决方案并没有解决我的问题 希望有人可以解决我应该做的事情以摆脱“AssociationTypeMismatch”
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
研讨会/ _form.html.erb。有哪些填充我的集合选择与名称和persion ID 可能的讲师。
....
<div class="field">
<%= f.label :presenter_id %><br />
<%= collection_select(:seminar,:lecturer,@current_presenters, :id, :fullName,{}, {:multiple=>true} ) %>
....
提交传递给我的控制器的参数
Parameters: {...., "seminar"=>{ "lecturer"=>["1", "2"], "title"=>"1234567890", "description"=>"ASDFGHJKL:"}, "commit"=>"Create Seminar"}
获取错误:
ActiveRecord :: AssociationTypeMismatch(讲师(#86075540)预期,得到字符串(#73495120)):.
答案 0 :(得分:1)
@seminar = Seminar.new
试试这个
@seminar.lecturer_ids = params[:seminar].delete(:lecturer)
@seminar.update_attributes(params[:seminar])