Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add roles to my users in rails app?

I have a simple app with an authentication system from devise. I tried adding roles to the user model. But nothing happens.

what i did was created the Role model and link it to the User Model:

$ rails g model Role name:string
$ rails g migration addRoleIdToUser role:references
$ rake db:migrate

(as directed by the devise wiki)

Then in my Models:

class User < ActiveRecord::Base
  belongs_to :role
end
class Role < ActiveRecord::Base
  has_many :users
end

Set up seeds.rb with my roles:

['seller', 'buyer', 'admin'].each do |role|
  Role.find_or_create_by({name: role})
end

Then

$ rake db:seed

That's all. And I also want to know how to make the user chose any of these roles at the time of signup

like image 251
Ahmed Reza Avatar asked Aug 22 '15 17:08

Ahmed Reza


People also ask

How do I add user roles?

To assign a user to a user roleIn the Administration pane, expand Security, and then select User Roles. In the User Roles pane, double-click Advanced Operators. In the Edit User Role dialog box, click Users. On the Users page, click Add.

Can we assign roles to users?

Assign roles in user profileYou can also assign roles to users from their individual profile page. Go to Dashboard > User Management > Users and click the name of the user. Click the Roles view, and click Assign Role. Choose the role you wish to assign and click Assign.

What are the user roles and define the use of the role?

Definition of user-defined user rolesA role is a database object that groups together one or more privileges and can be assigned to users. A user that is assigned a role receives all of the privileges of that role. A user can have multiple roles. A role hierarchy is also supported.


2 Answers

While you can add roles to your application like the way you did. But, it would be a lot of work to handle all types of roles and their responsibilities. Also, There are several nice gems like pundit and cancan which can be used to handle the authorization for your application users. They're very easy to integrate with your Rails application and works nicely with authentication gem devise which you are already using.

Here is a nice video tutorial which shows Rails Authorization with Pundit.

like image 171
K M Rakibul Islam Avatar answered Sep 19 '22 19:09

K M Rakibul Islam


First of all, instead of using an association, you can use enum in your user class:

class User < ActiveRecord:Base
   enum role: {seller: 0, buyer: 1, admin: 2}

   ... 
end

You'll need a migration to add a role (integer) column into your user's table.

In your terminal:

rails g migration add_role_to_users

Then edit the migration file:

class AddRoleToUsers < ActiveRecord::Migration
   def change
      add_column :users, :role, :integer
   end
end

Then you can e.g. use the SimpleForm gem to let the user choose his/her role during sign up:

<%=  simple_for for @user do |f| %>
   ...
   <%= f.select :role, collection: User.roles.keys.to_a %>
   ... 
<% end %> 

But SimpleForm is also good with associations:

<%= f.association :role, as: :radio_buttons %>

There are more examples for associations here.

like image 42
Fei Avatar answered Sep 21 '22 19:09

Fei