Rails 中嵌套对象的表单

发布于 2024-12-05 03:47:14 字数 3986 浏览 0 评论 0原文

我在创建表单来编辑 StudentReferrals 时遇到问题。每个学生都有一些基本信息,如姓名/生日/学生号。除了这些信息之外,学生还需要参加课程列表。我想要一张表格,允许某人填写学生信息,然后选择学生需要参加的最多 10 门课程。除了课程之外,每个“学生+课程”组合都有一个 credits_required 字段,用于说明学生需要修读课程的学分。

学生可以参加的课程列表来自 Course 模型

我有 3 个模型:

class StudentReferral < ActiveRecord::Base
  has_many :student_referral_courses, :dependent => :destroy
  has_many :courses, :through => :student_referral_courses

  # Fields: name, studentid, advisor_name, birthday
end

class StudentReferralCourse < ActiveRecord::Base
  belongs_to  :student_referral
  belongs_to  :course

  validates_presence_of :credits_required

  # Fields: (student_referral_id, course_id, credits_required)

  attr_accessor :course1, ..., :course9
  attr_accessor :credits1, ..., :credits9

  after_create  :add_courses

  def add_courses

    self.student_referral_courses.destroy_all

    unless self.course1.blank?
      self.student_referral_courses.create! :course_id => self.course1, :student_referral_id => self.id, :credits_required => self.credits1
    end
    unless self.course2.blank?
      self.student_referral_courses.create! :course_id => self.course2, :student_referral_id => self.id, :credits_required => self.credits2
    end
    ...
end

class Course < ActiveRecord::Base
  # Fields: department, name
end

我有一个用于创建/编辑学生推荐的表单,如下所示:

<%= form_for(@student_referral) do |f| %>
  <% if @student_referral.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@student_referral.errors.count, "error") %> prohibited this student referral from being saved:</h2>

      <ul>
      <% @student_referral.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>



<fieldset class="form">
<legend>Student Information</legend>
<table cellpadding="3" cellspacing="0" border="0" class="vert">
<tr><th class="subr"><%= f.label :studentid %></th><td><%= f.text_field :studentid %></td></tr>
<tr><th class="subr"><%= f.label :name %></th><td><%= f.text_field :name %></td></tr>
<tr><th class="subr"><%= f.label :birthday %></th><td><%= f.date_select :birthday, :start_year => 1990 %></td></tr>
<tr><th class="subr"><%= f.label :advisor %></th><td><%= f.text_field :advisor %></td></tr>
</table>
</fieldset>


<fieldset class="form">
<legend>Course Information</legend>
<span>You <b>MUST</b> select the credits required for each course.</span>
<ol>
<li><%= select(:student_referral, :course1, Course.all(:order => "dept,name ASC").collect {|p| [ "#{p.dept}: #{p.name}", p.id ] }, { :include_blank => '' })%>
    <%= select_tag 'student_referral[credits1]', options_for_select(['Select One',5.0,4.5,4.0,3.5,3.0,2.5,2.0,1.5,1.0,0.5]) %></li>
<li><%= select(:student_referral, :course2, Course.all(:order => "dept,name ASC").collect {|p| [ "#{p.dept}: #{p.name}", p.id ] }, { :include_blank => '' })%>
    <%= select_tag 'student_referral[credits2]', options_for_select(['Select One',5.0,4.5,4.0,3.5,3.0,2.5,2.0,1.5,1.0,0.5]) %></li>
    ...
</ol>
</fieldset>

<%= f.submit :class => "l-button black" %>

<% end %>

我的问题是我不能似乎创建或编辑任何这些记录,而无需使用验证进行可怕的黑客攻击。我有 10 个用于 course1course9attr_accessible 字段,以及我在 after_create 宏上使用的学分。但是,这不会让我看到课程中的任何验证错误(例如,如果他们没有提供任何学分),

我尝试使用 accepts_nested_attributes_for :student_referral_courses 但我似乎无法让它执行我的操作想。我真的很想用 Rails 的方式来做这件事,但我觉得我正在做一个糟糕的黑客。

I am having trouble creating a form to edit StudentReferrals. Each Student has some basic information like name/birthday/studentid. In addition to that information, students are referred to take a list of classes. I want to have a form that allows someone to fill out the student info, and then select up to 10 classes that the student is required to take. In addition to the classes, each "Student+Course" combo has a credits_required field that says how many credits of a class that student is required to take.

The list of classes that a student can take comes from the Course model

I have 3 models:

class StudentReferral < ActiveRecord::Base
  has_many :student_referral_courses, :dependent => :destroy
  has_many :courses, :through => :student_referral_courses

  # Fields: name, studentid, advisor_name, birthday
end

class StudentReferralCourse < ActiveRecord::Base
  belongs_to  :student_referral
  belongs_to  :course

  validates_presence_of :credits_required

  # Fields: (student_referral_id, course_id, credits_required)

  attr_accessor :course1, ..., :course9
  attr_accessor :credits1, ..., :credits9

  after_create  :add_courses

  def add_courses

    self.student_referral_courses.destroy_all

    unless self.course1.blank?
      self.student_referral_courses.create! :course_id => self.course1, :student_referral_id => self.id, :credits_required => self.credits1
    end
    unless self.course2.blank?
      self.student_referral_courses.create! :course_id => self.course2, :student_referral_id => self.id, :credits_required => self.credits2
    end
    ...
end

class Course < ActiveRecord::Base
  # Fields: department, name
end

And I have a form to created/edit the student referrals that is like the following:

<%= form_for(@student_referral) do |f| %>
  <% if @student_referral.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@student_referral.errors.count, "error") %> prohibited this student referral from being saved:</h2>

      <ul>
      <% @student_referral.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>



<fieldset class="form">
<legend>Student Information</legend>
<table cellpadding="3" cellspacing="0" border="0" class="vert">
<tr><th class="subr"><%= f.label :studentid %></th><td><%= f.text_field :studentid %></td></tr>
<tr><th class="subr"><%= f.label :name %></th><td><%= f.text_field :name %></td></tr>
<tr><th class="subr"><%= f.label :birthday %></th><td><%= f.date_select :birthday, :start_year => 1990 %></td></tr>
<tr><th class="subr"><%= f.label :advisor %></th><td><%= f.text_field :advisor %></td></tr>
</table>
</fieldset>


<fieldset class="form">
<legend>Course Information</legend>
<span>You <b>MUST</b> select the credits required for each course.</span>
<ol>
<li><%= select(:student_referral, :course1, Course.all(:order => "dept,name ASC").collect {|p| [ "#{p.dept}: #{p.name}", p.id ] }, { :include_blank => '' })%>
    <%= select_tag 'student_referral[credits1]', options_for_select(['Select One',5.0,4.5,4.0,3.5,3.0,2.5,2.0,1.5,1.0,0.5]) %></li>
<li><%= select(:student_referral, :course2, Course.all(:order => "dept,name ASC").collect {|p| [ "#{p.dept}: #{p.name}", p.id ] }, { :include_blank => '' })%>
    <%= select_tag 'student_referral[credits2]', options_for_select(['Select One',5.0,4.5,4.0,3.5,3.0,2.5,2.0,1.5,1.0,0.5]) %></li>
    ...
</ol>
</fieldset>

<%= f.submit :class => "l-button black" %>

<% end %>

My problem is that I cannot seem to create or edit any of these records without doing a terrible hack using the validation. I have 10 attr_accessible fields for course1 to course9 and same for credits that I use on an after_create macro. However, this does not let me see any validation errors from the course (like if they do not put any credits)

I tried using accepts_nested_attributes_for :student_referral_courses but I can't seem to get it to do what I want. I really want to do this the Rails way and I feel like I am doing a terrible hack.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

是伱的 2024-12-12 03:47:14

尝试将 has_many :through 添加到课程中,即

class Course class Course < ActiveRecord::Base
has_many :student_referrals, :dependent => :摧毁
has_many :student_referral_courses, :through =>; :student_referrals
结束

Try adding the has_many :through's to course i.e.

class Course < ActiveRecord::Base
has_many :student_referrals, :dependent => :destroy
has_many :student_referral_courses, :through => :student_referrals
end

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文