在我的Rails應用程序中,我想檢查通過表單發送的數額,然後將其保存到數據庫。如果金額太大,我想設置一個布爾變量「確認」爲false。否則,它的確認和真實。如果量< 9999,形式 - :rails before_save回調不會觸發
# if amount is too big, set to unconfirmed
before_save do
if self.amount > 9999
self.confirmed = false
else
self.confirmed = true
end
end
控制器動作(是腳手架):
def create
@statement = Statement.new(statement_params)
respond_to do |format|
if @statement.save
format.html { redirect_to thankyou_path, notice: 'Successfully created.' }
format.json { render action: 'show', status: :created, location: @statement }
else
format.html { render action: 'new' }
format.json { render json: @statement.errors, status: :unprocessable_entity }
end
end
end
測試這導致以下
我在我的模型中輸入這個得到保存,一切都很好。 - 如果金額> 9999,表單不會被保存。它只是停留在同一頁面上,沒有任何反應。沒有錯誤信息,除了數據沒有輸入數據庫之外,沒有什麼可以在日誌中看到。
我該怎麼做?
請分享你在哪裏試圖保存數據的控制器代碼。更新問題本身。 –
編輯。這是一個經典的腳手架邏輯。 – DonMB