4

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 .

Community
  • 1
  • 1

2 Answers2

2

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.

vipin
  • 2,374
  • 1
  • 19
  • 36
0

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

Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
  • I haven't used roles , both the models are basically seperate . And i created them using devise , does cancan cover more then one models generated by devise ? –  May 09 '14 at 10:53
  • You can define ability for each user based on their roles – Rajdeep Singh May 09 '14 at 10:56