![](/img/trans.png)
[英]Rails app does not recognize mass assigned belongs_to association in production
[英]Belongs to association works on local but not on production server
我有一張student
桌和一張classrooms
桌。 我的學生索引視圖中有一行代碼
<td><%= student.classroom.classroom_name %></td>
我正在嘗試顯示與student.classroom_id
相關聯的教室名稱。 此代碼使用sqlite3在本地開發環境中工作,但是當我使用MySQL將代碼復制到我的cPanel web服務器時,我收到此錯誤
undefined method `classroom_name' for nil:NilClass
有誰知道我的代碼有什么問題?
我已將本地安裝中的所有代碼復制到服務器。 我在遷移中添加了一個外鍵。 我在服務器上運行了rails db:migrate
。
classroom.rb
class Classroom < ApplicationRecord
validates :classroom_name, presence: true, length: { minimum: 3, maximum: 50}
has_many :students, dependent: :destroy
has_many :families, through: :students
end
student.rb
class Student < ApplicationRecord
enum status: [ :Active, :Deactive ]
belongs_to :classroom, optional: true
has_many :student_group_assignments
has_many :student_groups, through: :student_group_assignments
end
cPanel服務器undefined method
classes_name'為nil:NilClass`時出錯,但它確實適用於rails install上的本地ruby。
根據錯誤,您正在開始制作,顯然,表中沒有該學生的classroom
入口。 但是,如果是這種情況,您應該使用
<td><%= student.classroom.try(:classroom_name) %></td>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.