【问题标题】:Making multiselect work with simpleform, in rails在rails中使用simpleform进行多选工作
【发布时间】:2020-08-15 22:55:37
【问题描述】:

我的目标是让老师能够选择他们能够教授的多个课程,我使用的是简单表格。可以是复选框或标签

以下是我的设置。

教师档案模型

class TeacherProfile < ApplicationRecord
  belongs_to :subject 
  belongs_to :grade
  # belongs_to :curriculum
  has_many :curriculums
  belongs_to :user

  validates :national_identity, :curriculum_ids, :grade_id, :subject_id,
    :employer, :years_teaching, 
    :about_me, presence: true

  def self.teachers_classes(user_id)
    ClassRoom.where(user_id: user_id)
  end
end

课程模式

class Curriculum < ApplicationRecord
  has_many :student_profiles
  has_many :teacher_profiles
end

表格

.py-5.bg-light-v2
  .container
    .row.align-items-center
        .col-md-6
            %h2 Teacher Profile

%section.padding-y-50.border-bottom.border-light
  .container
    .row
      .col.col-md-8.mb-5
        = simple_form_for(@teacher_profile) do |f|
          = f.error_notification
          = f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present?

          .border-bottom.mb-4.pb-4
            %h5.mb-4 
              Personal Details
              %br
              %small.text-muted select what applies to you, tick if it applies to you.

            = f.input :national_identity, label: "Enter your National ID or Passort", placeholder:"216331771"
            = f.input :special_education, label: "I have Special education skills"
            = f.input :has_hardware, label: "I have a computer & Internet"
            = f.input :is_available, label: "I am available to tutor"
            = f.input :has_tsc_certificate, label: "I have a TSC certificate"
            = f.input :has_good_conduct, label: "I have  good conduct certificate"

          .border-bottom.mb-4.pb-4
            %h5.mb-4 Professional Details
            = f.input :years_teaching, label: "I have been teaching since"
            .row
              -# .col= f.association :curriculum, prompt: "Select the curriculum you use"
              .col= f.association :curriculums, input_html: {:multiple => true} 
              .col= f.association :grade, prompt: "Select the grades you teach"
            = f.association :subject, prompt: "Select the subjects you teach"
            = f.input :employer, label: "I currently work at", placeholder:"Enter name of employer if applicable or leave blank"

          .border-bottom.mb-4.pb-4
            %h5.mb-4 About your passion
            = f.input :about_me, as: :text, label: "Tell us why you love teaching", placeholder:"I am passionate about teaching..."

          .form-actions
            = f.button :submit, class: "btn btn-primary btn-lg mt-2"

我收到一个错误:

Curriculum ids can't be blank

我可能会错过什么?

【问题讨论】:

  • 请阅读“MCVE”。我们需要能够运行代码来生成错误。
  • 您的两个模型都有has_many 关联。要使其正常工作,您需要改用has_and_belongs_to_many
  • 谢谢@theTinMan 我什至不知道它的存在。学到了很多。
  • @Vasilisa 我带着一张加入的桌子去了,就像一个魅力。也会考虑使用 has_and_belongs_to_many。谢谢

标签: ruby-on-rails ruby haml simple-form


【解决方案1】:

在模型中,您应该验证curriculums,而不是curriculum_ids

【讨论】:

    【解决方案2】:

    建立一个多对多关系的连接表。这将问题分开,允许添加额外的字段而无需太多工作。

    创建迁移表/模型 X,并引用所需的字段,在这种情况下使用 subjectscurriculumgradesteacher_profiles

    在此表中创建多对多关系,然后将新模型嵌套在teacher_profiles 下。

    视频“Rails - Nested Forms in Rails | [Tutorial]”很有用。

    【讨论】:

    • 请在答案中添加更重要的代码部分。
    • 你的回答是一个开始,但是,正如@Vasilisa 所说,总结这些变化并解释它们为什么有帮助。对您所做更改的描述不如代码有用。另外,请记住,答案要简洁明了。我删除了很多健谈的“绒毛”,因为它在 SO 上并不是很理想。请记住,SO 就像一本参考书;我们不那么正式,但我们想要清晰简洁的信息。
    猜你喜欢
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多