0
votes

I need a field to populate a default value for a new record, but not on edit. I'm using haml and simple_form. This works, but feels inelegant. I'm sure there's a way to do this on a single line and avoid all the repetition but I don't know how to do the inline check for a new record?

  - if @employee_expense.new_record?
    #amount_field= f.input :amount, :input_html => {value: '0.00', :maxlength => 6}
  - else
    #amount_field= f.input :amount, :input_html => { :maxlength => 6}

Any thoughts?

1
awww haml i use erb but will like to tell that other idea can be @employee_expense.persisted? - aelor

1 Answers

2
votes

I would do it in NEW action of the Controller. In this case you do not need any ifs in your view, what is good You can simply do something like this:

def new
 @employee_expense = EmpolyeeExpense.new(amount: 0) # EmpolyeeExpense.new(:amount => 0.0) for ruby 1.9 and lower
end