0

我有 3 个这样的模型:

class CertificatorBtwInstructor < ActiveRecord::Base
    belongs_to :certificator
    belongs_to :instructor
    # other code ...
end

class Certificator < ActiveRecord::Base
    has_many :btw_instructors, :class_name => "CertificatorBtwInstructor"
    has_many :instructors, :through => :btw_instructors
    # other code ...
end

class Instructor < ActiveRecord::Base
    has_many :btw_certificators, :class_name => "CertificatorBtwInstructor"
    has_many :certificators, :through => :btw_certificators
    # other code ...
end

在我的讲师新表格上,我设置了:

= simple_form_for( [:cm, @instructor], :html => { :class => "fAdm" } ) do |f|
    = f.object.errors
    - others fields ...
    = f.input :certificator_ids, :required => false, :collection => Certificator.choices_relation_select, :input_html => { :multiple => true, :size => 12 }
    %div.row.buttons
        = f.button :submit

然后,当我提交表单而不从“certificator_ids”输入集合中选择任何证书时,新记录的创建没有问题。但是,当我在“certificator_ids”输入集合中选择任何项目时,会出现一个错误(我用 = f.object.errors 将其可视化),错误是这样的:

{:certificators=>["is not valid"]} 

但在我的表单上,我没有设置“验证者”字段,所以我不明白他们为什么得到这个属性。

4

1 回答 1

0

将其更改为

<%= f.association :certificators, :as => :check_boxes %>

这将在复选框上向您显示所有验证者,您应该看看这个 https://github.com/plataformatec/simple_form 和这个

http://railscasts.com/episodes/47-two-many-to-many

于 2012-10-11T05:13:00.080 回答