Question

I'm new to ruby and rails, and I'm trying to make the available roles to be shown in a dropdown list, (It used to work when I made it manually, however I recently discovered rails_admin), I've read a lot about it, (I don't have a Roles model or controller, since the Roles won't ever change and one user can only have 1 role).

Note: I've installed Devise and CanCan gems as well.

So I have this constant ROLES on my user.rb model, that stores the roles and I'd like that when I'm creating or editing a user with rails_admin, the role instead of a simple input field, shows a dropdown list populated by that constant named ROLES.

This is what I have so far:

class User < ActiveRecord::Base
  ROLES = %w(Admininistrator C1 D1 M1 M2).freeze
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable


  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :role

  #Tried this, no luck
 def role_enum
   [['Administrator'],['C1'],['D1'], ['M1'], ['M2']]
 end
 #And this, also no luck
 def ROLES_enum
   [['Administrator'],['C1'],['D1'], ['M1'], ['M2']]
 end
end

And also tried uncommenting this on the rails_admin.rb file

config.model 'User' do
   configure :role, :string 
end

No luck, Like I said, I'm new to both Ruby and Rails, but I've had some experience with MVC and PHP's framework Symfony, but this is completely new to me. I'd appreaciate any help I could get.

Was it helpful?

Solution

Try this:

def role_enum
  ['Administrator','C1','D1','M1', 'M2']
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top