4
votes

I have 2 types of users , and generated devise models and views for them . Had to do this basically because using roles was out of question here . Both Users are different and will have different dashboards . Also have to store there data in different tables as those will be further processed on . I was successful in creating different routes for both of them but now as i plan to move forward , i want to follow a definite structure which won't cause problems later . I went the polymorphic way to associate the models and also namespaced them as User A and User B.

User A

Will be able to post queries

Will be able to release payments for B

Will be able to keep and remove jobs

User B

Will just be able to answer queries

Will receive payments on completing job

Will be able to view job

*Note - These are just some of the functions of the many i have to accomplish .

I have looked into other questions at SO but i didn't get a concrete answer . Looking forward to suggestions from those who might have achieved the same or have been in a similar situation . Thanks in advance .

2

2 Answers

2
votes

Firstly use CanCanCan for permission

add role column into your database, and generate ability model

define role into your user model

  enum role: [:Admin, :Enduser, :Moderator]

for different dashboard

Make dashboard Controller

class DashboardController < ApplicationController
  def index
  end
end

in your route make root dashboard

root 'dashboard#index'

in views/dashboard/index.html.erb

  <% if current_user.role == "Admin" %>
  <%= render 'admin' %>
  <% elsif current_user.role == "Moderator"  %>
  <%= render 'moderator' %>
  <% elsif current_user.role == "ClientUser" %>
    <%= render 'client_user' %>
  <% end %>

in views/dashboard/index.html.erb make file like

_admin.html.erb
_moderator.html.erb
_client_user.html.erb

and insert different views into different file as per your requirement.

0
votes

You need to use CanCan gem for authorization, you can go through it's documentation and railscasts video tutorial following the links.