sasdev
sasdev

Reputation: 506

Activeadmin set default date

When a new record is created I want to set the default date to a month ahead.

With other fields the default is set in the migration, but where would you set it in this situation?

enter image description here


Update

I tried to add it to

models/invoice.rb

 class Invoice < ActiveRecord::Base
 before_create :set_due_date

 private
   def set_due_date
      self.due_date = DateTime.now + 30
   end
 end

admin/invoice.rb

form do |f|
   f.inputs "Options" do
      f.input :due_date, :as => :datepicker
   end
end

Migration

create_table :invoices do |t|
   t.datetime :due_date
end

Upvotes: 1

Views: 1680

Answers (2)

sasdev
sasdev

Reputation: 506

I ended-up using initialize. This caused the value to be set before the page is rendered.

after_initialize :set_due_date

 def set_due_date
   self.due_date ||= DateTime.now + 30
 end

Upvotes: 1

jgraft
jgraft

Reputation: 938

If I am understanding correctly, you should be able to add a callback in the Model of whatever the "due_date" field is in. Something like this maybe:

before_create :set_due_date

def set_due_date
  self.due_date = DateTime.now + 30
end

Upvotes: 2

Related Questions