Showing drop down list box for a field in RailsAdmin


RailsAdmin is really good if you want to create a out-of-the-box admin panel for your rails based application. It will generate a nice admin panel based on your models. Though, to me, it is not as intuitive as that of Django, it is very useful.

RailsAdmin can create several types of form fields like Text Fields, Text Area, Date Picker, Checkbox etc. based on your column’s type. However, sometime we may need to override it’s default behavior. For example, we may need to show a drop down list box for a field instead of text field.

Let’s assume we want to show a drop down list for status column of User model. So, open your user.rb from models directory and add the following method to the class definition.


def status_enum
   [['Active'],['Pending'],['Banned']]
end



Now you will see a list box for Status field when you go to Users section of Rails Admin. However,  if we can store different values in the database other than the strings shown in the list. Let’s modify our code to something like:
Continue reading →