我的標準守則Rails的5 +嵌套屬性具有errror上HAS_ONE創建
class Bank < ApplicationRecord
has_one :address
accepts_nested_attributes_for :address
end
class Address < ApplicationRecord
belongs_to :bank
end
我的控制器
def create
@bank = Bank.new(bank_params)
respond_to do |format|
if @bank.save
format.html { redirect_to @bank, notice: 'Bank was successfully created.' }
format.json { render :show, status: :created, location: @bank }
else
format.html { render :new }
format.json { render json: @bank.errors, status: :unprocessable_entity }
end
end
end
def bank_params
params.require(:bank).permit(:code, :currency, :name, :mobile_1, :mobile_2, :email, address_attributes: [:id, :name, :area, :pin_code, :city_id])
end
它給錯誤類似的東西
@信息= {」 address.bank「=> [」must exist「]},@details = {」address.bank「=> [{:error =>:blank}
爲什麼它顯示反向...不理解
地址模型是什麼樣的。在我看來,你有評估銀行的評估驗證,看起來像是失敗了。因此,你看到它在反向 – Doon