【问题标题】:simple_form, bug with :has_many :through relation?simple_form,带有 :has_many :through 关系的错误?
【发布时间】:2011-12-16 20:26:53
【问题描述】:

我有 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

在我的 Instructor 新表格上我设置了:

= 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"]} 

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

【问题讨论】:

    标签: activerecord has-many simple-form


    【解决方案1】:

    改成

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多