Daryll Santos
Daryll Santos

Reputation: 2081

Rails - Rendering different "dashboard/user profile" depending on the type of user?

My app has different user types/model, ex: Doctor and MedicalInstitution (not sure if those are good names btw). They have polymorphic association to User.

The controllers so far:

HomesController - Verify if user is logged in or not. If yes redirect to dashboard_path if not redirect to landing page.

DashboardsController - "Display the current user profile." Code:

class DashboardsController < ApplicationController
  before_filter :authenticate_user!
  def show
    @user = current_user.profile
    render "#{@user.dashboard_something_variable}_dashboard"
  end
end

Is this a good idea, or would you split the controllers? I feel that DoctorsController's show action would be for other people to view radiologist profiles, not for the Doctor himself to view his profile/private things.

Thank you!

Upvotes: 1

Views: 487

Answers (1)

crispychicken
crispychicken

Reputation: 2662

You could set up a partial for each view..

def show
  # ...
  @partial = current_user.profile # 'doctor', 'radiologist',...
end

In show.html.erb:

<%= render @partial %>

Then you would save each view partial in the controller's views folder, e.g. _doctor.html.erb, _radiologist.html.erb.

Upvotes: 1

Related Questions