Index pages admin / user

What’s the best way to deal with 2 views of an index page from the same controller? I would like to split it up between an admin index and user index page.

Is a partial my only option here?

Are you wanting to render a different layout? You could do something like this:

class MyController < ApplicationController
  layout :resolve_layout

  # ...

  private

  def resolve_layout
    case action_name
    when "new", "create"
      "some_layout"
    when "index"
      "other_layout"
    else
      "application"
    end
  end
end

You can also look into nested layouts here:

1 Like

In the long run I found out it was easier to have different controllers that respond to different urls.

/groups shows you your groups that you are enrolled into while /admin/groups is only for admins that need to see all the groups.

In this ways there are two controllers with two methods and two views and they share a common partial but it is easier to add new things to the admin view without all the ifs for if you are currently admin or not.

1 Like