我有下一个要保存的代码:
Transaction.create(:status => params[:st], :transaction_id => params[:tx], :purchased_at => Time.now).save!
但是,如果出现ActiveRecord::RecordNotUnique
错误,如何重定向到主根页?我可以抓住这个错误吗?
答案 0 :(得分:0)
就像你捕捉其他每一个错误一样
begin
Transaction.create!(:status => params[:st], :transaction_id => params[:tx], :purchased_at => Time.now)
rescue ActiveRecord::RecordNotUnique
redirect_to root_path
end