Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select enum from form to set role

Ruby on Rails 4.1

I am using Devise with enum role. It currently sets a defualt role when the User is created. I want to add a field to the form that creates Users to set the enum role.

I read this but it doesn't say how to utilize the new roles.

This is the User class

devise :database_authenticatable, :registerable, :confirmable,
     :recoverable, :rememberable, :trackable, :validatable
enum role: [:user, :vip, :admin, :developer, :marketing, :support, :translator]
after_initialize :set_default_role, :if => :new_record?

def set_default_role
  self.role ||= :user
end

This is the part of the form where I am trying to have a select to pick an enum role:

<div class="form-group">
  <%= f.collection_select :role, User.roles, :id, :enum, {prompt: "Select a role"}, {class: "form-control input-lg"} %>
</div>

The error:

NoMethodError - undefined method `enum' for ["user", 0]:Array:
actionview (4.1.1) lib/action_view/helpers/form_options_helper.rb:761:in `value_for_collection'

I have never used enum before and the documentation is not proving helpful. How do I make the enum options show?

like image 494
DDDD Avatar asked Jun 12 '14 21:06

DDDD


People also ask

How to SELECT enum in mysql?

This means that you can use the following SELECT statement to find rows into which invalid ENUM values were assigned: mysql> SELECT * FROM tbl_name WHERE enum_col=0; The index of the NULL value is NULL . The term “index” here refers to a position within the list of enumeration values.

How to Get values from enum?

Get the value of an Enum To get the value of enum we can simply typecast it to its type. In the first example, the default type is int so we have to typecast it to int. Also, we can get the string value of that enum by using the ToString() method as below.

What is enum data type syntax?

Enumeration or Enum in C is a special kind of data type defined by the user. It consists of constant integrals or integers that are given names by a user. The use of enum in C to name the integer values makes the entire program easy to learn, understand, and maintain by the same or even different programmer.

What is enum in database?

Enumerated (enum) types are data types that comprise a static, ordered set of values. They are equivalent to the enum types supported in a number of programming languages. An example of an enum type might be the days of the week, or a set of status values for a piece of data.


2 Answers

To start, enum is not the name of an attribute. The name of the attribute is role.

Take a look at the rails-devise-pundit example application, specifically the file app/views/users/_user.html.erb which is a partial that creates a form to allow the administrator to change a user's role. I doubt you want to use a collection_select for helper (that is suitable if you have a separate Role model). Instead, an ordinary select form helper will work.

Here's a simple example that hardcodes the role options:

<%= f.select(:role, [['User', 'user'], ['Vip', 'vip'], ['Admin', 'admin']]) %>

Here is a better example that avoids hardcoding the roles in the form:

<%= f.select(:role, User.roles.keys.map {|role| [role.titleize,role]}) %>

The statement obtains an array of roles from the User model and constructs an array of key-value pairs using the map method.

like image 51
Daniel Kehoe Avatar answered Sep 17 '22 21:09

Daniel Kehoe


Since you are using Rails 4 or higher, enums are even less complicated.

Given the following enum:

enum role: {
  admin: 1
}

Enums expect the HTML option attribute value to be the enum key:

<option value="admin"> <!-- As opposed to: <option value="1"> -->

Knowing this, you can pass in the enum keys.

<%= f.select :role, User.roles.keys, {}, class: 'user-roles-select' %>

Then using CSS you can modify the appearance.

.user-roles-select option {
  text-transform: capitalize;
}
like image 31
Mohamad Avatar answered Sep 20 '22 21:09

Mohamad