이 내 모델입니다 :collection_select를 사용하여 다형 모델을 만드는 방법은 무엇입니까?
class Speaker < ActiveRecord::Base
belongs_to :session, :foreign_key => :session_id, :class_name => :Session
belongs_to :speakable, :polymorphic => true
end
class Session < ActiveRecord::Base
has_many :speakers
accepts_nested_attributes_for :speakers
end
class Person < ActiveRecord::Base
has_many :speakers, :as => :speakable
end
class Company < ActiveRecord::Base
has_many :speakers, :as => :speakable
end
내가 지금하고 싶은 것은이 같은 것입니다 : 응용 프로그램/조회/세션/edit.html.erb
<% f.fields_for :speakers do |sf| %>
<p>
<%= sf.label :speaker %><br />
<%= sf.collection_select :speakable, Company.all + Person.all, :id, :full_name %>
</p>
<% end %>
그러나이 때문에 작동하지 않습니다 다형성 할당. 이 문제에 어떻게 접근합니까?
편집 : 오류 : PARAMS의 존재와
undefined method `base_class' for String:Class
:
"speaker"=>{"speakable"=>"1063885469", "session_id"=>"1007692731"}
읽기 가능해 전달 값은 스피커/회사의 ID입니다. 예, 반환 할 값은 collection_select
입니다.하지만 두 값 (speakable_id
및 speakable_type
)을 모두 어떻게 관리 할 수 있습니까?
어떤 오류가 있습니까? – shingara