在我的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