How can this simple new action be refactored?
def new
@payment = Payment.new(:invoice_id => params[:invoice_id])
if @payment.invoice.present?
@payment.amount = @payment.invoice.balance.abs
end
@title = "New payment"
end
It feels a bit clumsy to me.
Thanks for any help!
To simplify your controller, you should move the business logic to your model.
Here’s three suggestion:
If invoice_id will not change over the live of your Payment instance, and you don’t need caching:
If invoice_id will not change over the live of your Payment instance, but you use the amount value multiple times in your controller/view. (use caching):
If invoice_id may change over the live of your Payment instance, and you need caching: