user664833
user664833

Reputation: 19525

How to configure rails_admin to change integer input field into select field?

I am using rails_admin with devise. I have a plain User model (having the typical devise attributes) as well as an additional integer type attribute called role_type. When I view http://0.0.0.0:5000/admin/user/1/edit in the browser, I see ten devise attributes, plus my Role type (though it is an type="number" input field) whereas I want it to be a select drop down menu. I have done some searching around, and have added a configuration to rails_admin.rb as well as a corresponding partial, and though I get the drop down I want, it's the only thing I get for the whole user edit view.

I want to configure the way rails_admin displays the :role_type field, while (obviously) also being able to edit the rest of the User fields.

db/migrate/20131119000715_add_role_type_to_user.rb

class AddRoleTypeToUser < ActiveRecord::Migration
  def change
    add_column :users, :role_type, :integer, null: false, default: 0
  end
end

app/models/user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  ROLES = ['default', 'admin', 'moderator']
end

Gemfile

gem 'rails_admin', github: 'sferik/rails_admin'

config/initializers/rails_admin.rb

RailsAdmin.config do |config|
  config.model User do
    edit do
      field :role_type do
        partial 'form'
      end
    end
  end
end

app/views/rails_admin/main/_form.html.erb

<%= form.select :role_type, User::ROLES.each_with_index.map { |e, i| [e, i] } %>

Upvotes: 3

Views: 5474

Answers (2)

Daniel Bang
Daniel Bang

Reputation: 725

You can create a role_type_enum method in your model that returns User::ROLES.

https://github.com/sferik/rails_admin/wiki/Enumeration

Upvotes: 3

askprod
askprod

Reputation: 129

I know this is a little late but I came accross this problem today, and found a solution! If anyone stumbles on this: by changing field: you_column_name do to configure: your_column_name do in your rails_admin initializer, it should still show all your other fields and change only the one specified.

Thanks to the doc: https://github.com/sferik/rails_admin/wiki/Fields

Upvotes: 1

Related Questions