2016-11-28 97 views
0

我使用導軌5.0.0.1爲相關領域的強制驗證

當我提交表單,爲相關領域的驗證即將投入行動。

千兆用戶類別等車型

我使用設計用戶認證

千兆模型

class Gig < ActiveRecord::Base 
    has_many :proposals 
    belongs_to :category 
    has_many :abilities 
    has_many :skills, through: :abilities 
    belongs_to :user 
end 

用戶模型

class User < ApplicationRecord 
    # Include default devise modules. Others available are: 
    # :confirmable, :lockable, :timeoutable and :omniauthable 
    devise :database_authenticatable, :registerable, 
     :recoverable, :rememberable, :trackable, :validatable 

    has_many :gigs 
    has_many :proposals 
end 

分類模型

class Category < ActiveRecord::Base 
    has_many :gigs 
end 

當我嘗試創建控制檯的演出,事務回滾。

錯誤消息

[ 「類別必須存在」, 「用戶必須存在」]

我感謝你的幫助。提前致謝。

+0

您是否發佈了gig.rb的完整內容? –

+1

是的,這是在鐵軌標準5.什麼問題?如果問題出現在rails控制檯中,爲什麼在這篇文章中包含控制器和視圖信息? – Anthony

+0

謝謝。我不知道它是標準的。我最近升級到軌道5 –

回答

2

在軌5,當您添加belongs_to的它使該領域所需。試試這個

belongs_to :user, optional: true 
+0

感謝您的答案。有效 –