Chris Muench
Chris Muench

Reputation: 18338

Ruby On Rails different views for same action (Based on User's role)

I have a ruby on rails controller that will display a different form for a logged out user than a logged in user.

What is the best way to approach this? (Is the below way ok?)

   class UsersController < ApplicationController
      def index
        if logged_in && is_admin
          render 'admin_index'
        end
        #use default index
      end
    end

Upvotes: 3

Views: 3487

Answers (2)

Ashitaka
Ashitaka

Reputation: 19203

You can always do this:

if condition
render :page and return

Just as you can do this:

if condition
redirect_to and return

Upvotes: 1

macarthy
macarthy

Reputation: 3069

Sure thats fine except you might get a 'cannot render action twice' type error (if im admin and logged in it still would try to render the default after rendering the admin action)

 class UsersController < ApplicationController
      def index
        if logged_in && is_admin
          render 'admin_index'
        else
          render 
        end

      end
    end

might be better

Upvotes: 5

Related Questions