使用Rails创建类似电子表格的表单

时间:2016-07-26 18:30:56

标签: mysql ruby-on-rails

我现在已经学习Rails大约两个月了。我正在为教师创建一个应用程序来跟踪学生的进度。我已经获得了#34;任务"教师为教室添加新作业的模型,我已经得到了"用户"模型工作,以便教师和学生都是可以登录到应用程序的用户。还有一个"课堂"模型,每个教室都有很多学生和很多作业。

其中一个主要观点需要以传统教师成绩簿计划为特色的电子表格形式。电子表格将使用学生作为行和作业列。电子表格中的每个单元格都代表学生在该作业中的分数。

根据我迄今所学到的知识,我认为我的下一步应该是创建一个链接学生和作业的联接表,第三列为"得分"。

我难倒的部分是创建表单,以便输入单元格与"得分相关联。联接表中的列,以便输入新号码将更改该作业的学生分数。

我确信文章或教程必须存在于这个概念的某个地方,但我还没有找到任何东西。至少,没有一个我认为是这个目标的解决方案。

提前感谢您的任何指导。

更新包含型号代码

用户模型:

class User < ApplicationRecord
    attr_accessor :remember_token, :activation_token, :reset_token
    before_save   :downcase_email
    before_create :create_activation_digest

    has_many :seminars, dependent: :destroy

    # Neccessary for finding all classes that a student is enrolled in
    has_many :aulas, dependent: :destroy,
                        foreign_key: :student_id


    validates :first_name, length: {maximum: 25},
            presence: true
    validates :last_name, length: {maximum: 25},
            presence: true
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    validates :email, presence: true, length: { maximum: 255 },
                    format: { with: VALID_EMAIL_REGEX },
                    uniqueness: { case_sensitive: false }

    has_secure_password

    validates :password, presence: true, length: {minimum: 6}, allow_nil: true

   ### Several methods that I omitted to keep the question shorter

end

研讨会型号: (A&#34;研讨会&#34;是一个课时,但我想避开这个词,&#34; Class&#34;因为我认为这会导致错误。)

class Seminar < ApplicationRecord
  belongs_to  :teacher, class_name: "User",
                        foreign_key: "user_id"
  has_many    :aulas, dependent: :destroy
  has_many    :students, through: :aulas, source: :student
  has_many    :assignments

  validates :user_id, presence: true
  validates :name, presence: true, length: { maximum: 40 }
end

Aula模型: (Aula在课堂上是西班牙语。再一次,我想避免使用这个词,&#34; Class&#34;。这个模型在学生用户和研讨会之间建立了一种关系(课堂时期)。&#34;

class Aula < ApplicationRecord
    # Aula is the relationship between a student and a classperiod.

    belongs_to :student, class_name: "User"
    belongs_to :seminar

    validates :student_id, presence: true
    validates :seminar_id, presence: true
end

作业模式:

class Assignment < ApplicationRecord
    belongs_to :seminar

    validates :name, presence: true, length: { maximum: 40 }
    validates :seminar_id, presence: true
    validates :possible, presence: true
end

2 个答案:

答案 0 :(得分:3)

我建议你在表格中显示用户x分配并使用in place edit,这样用户就可以点击单元格并在那里编辑它。对于铁轨,你有一个名为&#34;最好的宝石&#34; (https://github.com/bernat/best_in_place)可以解决这个问题(那里还有一个使用它的热门演员:http://railscasts.com/episodes/302-in-place-editing?view=asciicast)。希望它有所帮助,谢谢

编辑: 回答你的问题,我将best_in_place用于项目经理,并且表现非常好。看起来你正在使用microsoft excel或其他东西进行编辑。 关于后端:嗯,你在学生和作业之间有一个n x n的关系。例如,您需要assignments_student模型,该模型同时属于您的userassignment模型,并且还具有score(请查看nxn关系)如果您有疑问)。因此,assignments_student联结表上的每一行(对用户和分配表都有两个外键加上score属性)将成为您桌面上的一个单元格,其方式是您正在编辑的值相应用户/分配的score属性。

希望我说清楚。祝你好运!

答案 1 :(得分:1)

您可能对cocoon感兴趣,它是一个允许您这样做的宝石:

  

使用jQuery的动态嵌套表单变得简单

这允许你添加&#34;行&#34;根据学生人数动态显示您的电子表格形式。

另请阅读有关Rails accepts_nested_attributes_for的内容,这是允许执行嵌套表单的基础。