- 
                Notifications
    
You must be signed in to change notification settings  - Fork 2.3k
 
How to set default values
RailsAdmin always adds a blank entry in drop-downs, so if you are adding a new record using Rails Admin, it will be blank by default. To pre-select a default value however, e.g.please do it in your model definition, at initialization time:
class Team < ActiveRecord::Base
  ....
  after_initialize do
    if new_record?
      self.color ||= 'red' # be VERY careful with ||= and False values
    end
  end
  def color_enum
    ['white', 'black', 'red', 'green', 'blue']
  end
   ...
endThe "after_initialize" hook (or callback) triggers after a model record is instantiated. It will set the color value to 'red' for a new record and will leave it to its current value for an existing record (in the edit view).
Adding current_user as a default value adds an extra challenge.  The controller layer knows about current_user, this information is not typically available in the model layer for use by after_initialize.  And this is an intentional implication of the MVC architecture.
In RailsAdmin, you can assign a default value of current_user like this:
config.model Post do 
  edit do 
    field :user_id, :hidden do
      default_value do
        bindings[:view]._current_user.id
      end
    end
  end 
endThis was taken from these discussion threads:
- http://groups.google.com/group/rails_admin/msg/fe588202e4401dc4
 - http://groups.google.com/group/rails_admin/msg/5338518c540f9151
 
Some refinement may be required to avoid resetting the user of an existing item, if that's not desired.