2016-07-26 70 views
1

我已經學習Rails大約兩個月了。我正在爲教師創建一個應用程序,以跟蹤學生的進度。我已經有了「作業」模型,可以讓教師爲課堂添加新作業,並且我已經有了「用戶」模型,以便教師和學生都可以登錄到應用程序。還有一個「課堂」模式,每個教室都有多個學生和多個作業。使用Rails創建電子表格式表單

其中一個主要觀點需要像傳統教師成績冊程序那樣具有電子表格形式。電子表格將使用學生作爲行和分配作爲列。電子表格中的每個單元格都將表示該作業的學生分數。

從我到目前爲止所瞭解到的情況來看,我認爲我的下一步應該是創建一個鏈接學生和作業的連接表,以及第三列「分數」。

我被難倒的部分是創建表單,以便輸入單元格與連接表中的「分數」列相關聯,以便輸入新數字將更改該作業的學生分數。

我確定文章或教程必須存在某個地方爲這個概念,但我還沒有找到任何。至少,我沒有認識到這個目標的解決方案。

預先感謝您的任何指導。

更新,包括代碼適用機型

用戶模型:

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+\-.][email protected][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「研討會​​」是一類的時期,但我想避免的話,「類」,因爲我認爲這會導致錯誤)

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 

禮堂型號:。 (禮堂是西班牙語類同樣,我想避免的話,「類」這個模式升創建學生用戶和研討會(節課的時間)之間的關係「

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 
+0

如果您可以編輯問題並向我展示您的模型(學生,作業和分數)及其關係,我可以更輕鬆地爲您提供幫助,謝謝 –

+0

Haider,謝謝您的建議。我更新了問題以包含所有模型。我還沒有「Score」的模型。我目前的計劃是將其包含在學生/作業連接表中。但我願意改變這一點。 –

回答

1

我建議你在一個表格中顯示Users x Assignments並使用in place edit,這樣用戶可以點擊單元格並在那裏編輯它的值。對於導軌,你有一個稱爲「最佳位置」的寶石(https://github.com/bernat/best_in_place),它可以實現這個技巧(還有一個軌道投射,顯示熱門使用它:http://railscasts.com/episodes/302-in-place-editing?view=asciicast)。希望它有幫助,謝謝

編輯: 回答你的問題,我使用best_in_place項目經理,它的表現真的很好。看起來你正在編輯微軟的Excel或其他東西。 關於後端:那麼,你在學生和作業之間有一個n×n的關係。例如,您需要一個assignments_student模型,該模型既屬於您的user也屬於assignment模型,並且還包含score(如果您有疑問,請查看nxn關係)。因此,您的assignments_student聯結表(其中包含用戶和賦值表的兩個外鍵以及score屬性)中的每一行都將成爲表格上的一個單元格,您可以編輯該用戶/作業的score屬性值。

希望我明確。祝你好運!

+0

我在尋找解決方案時注意到其他地方的寶石。我會看着投下的鐵軌。謝謝! –

+0

在看這個Railscast之前,我打算使用表單域,老師可以輸入分數。就地編輯功能看起來更加用戶友好,但我想知道它將如何影響性能。這種方法不會使應用程序在每次編輯時都對服務器產生影響嗎?與傳統的成績簿應用程序相反,老師在其中進行許多更改,然後單擊「保存」按鈕。 另外,如果你不介意。我仍然被我原來的問題難住:我如何編寫表單的後端,以便每個輸入框都綁定到連接表中的學生/賦值行?再次感謝。 –

+0

@JeffZivkovic編輯了我的答案。希望能幫助到你! –

1

您可能會感興趣的cocoon,它是一種寶石,讓你做:使用jQuery

動態嵌套形式變得容易

這允許您根據學生人數將「行」動態添加到類似電子表格的表單中。

另請參閱Rails的accepts_nested_attributes_for,這是允許嵌套窗體的基礎。

+0

我會研究這兩個。謝謝。 –