simple_form,bug with:has_many:通过关系?

时间:2011-12-16 20:26:54

标签: activerecord has-many simple-form

我有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的情况下提交表单时,创建的新记录没有问题。 但是,当我在'certificator_ids'输入集合中选择任何项目时,会出现错误(我使用= f.object.errors将其可视化),错误是这样的:

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

但是在我的表单上我没有设置字段'certificators',所以我不明白为什么他们有这个属性。

1 个答案:

答案 0 :(得分:0)

将其更改为

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

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

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