simple_form, ошибка с: has_many: через отношение? - PullRequest
0 голосов
/ 17 декабря 2011

У меня есть 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

затем, когда я отправляю форму без выбора какого-либо Сертификатора из входной коллекции «attributeator_ids», новая запись создается без проблем. Но когда я выбираю какой-либо элемент в коллекции входных данных «certificateator_ids», появляется ошибка (я визуализирую ее с помощью = f.object.errors), и эта ошибка выглядит так:

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

но в моей форме я не установил поле 'Certificators', поэтому я не понимаю, почему они получили этот атрибут.

1 Ответ

0 голосов
/ 11 октября 2012

Измените его на

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

Это покажет вам все ваши сертификаты на флажках, вы должны взглянуть на это https://github.com/plataformatec/simple_form и это

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

...