2016-11-15 8 views
2

私はRails 5.0.0を使用しています。私は、このモデルbelongs_toフィールドをオプションにするにはどうすればよいですか?

class Scenario < ApplicationRecord 
    belongs_to :grading_rubric 
    has_many :confidential_memo 
end 

を持っているが、私はモデルのための私の作成メソッドを呼び出すとき、それは私がbelongs_toのがあることを示すにはどうすればよい私が手にエラーが

full messages: ["Grading rubric must exist"] 

ある

def create 
    @scenario = Scenario.new(scenario_params) 
    respond_to do |format| 
     if @scenario.save 
     puts "saved successfully." 
     format.html { redirect_to confidential_memo_path(@scenario), notice: 'Saved successfully.' } 
     else 
     puts "full messages: #{@scenario.errors.full_messages}" 
     format.html { render action: "show" } 
     end 
    end 
    end 

失敗します引数はオプションである必要があります(つまり、nullにすることができます)。

答えて

2

これまでのように値をnilのままにして、Railsが完全に満足していることが前提でした。

belongs_to :grading_rubric, optional: true 

あなたはそれhere

についての詳細情報を見つけることができます。しかし、これはあなたが belongs_toはオプションになりたい場合は、あなたは、単に optional: trueを渡す必要がRailsの5

に変更されました

関連する問題