Ruby on Rails different views for one action (based on user role)

I have a rails ruby ​​controller that displays a different form for a user who is logging out than a registered user.

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

   class UsersController < ApplicationController
      def index
        if logged_in && is_admin
          render 'admin_index'
        end
        #use default index
      end
    end
+3
source share
2 answers

I am sure that this is fine, except that you can get an error like "do not display the action twice" (if im admin is logged in, he will still try to display the default value after rendering the administrator's action)

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

      end
    end

could be better

+5
source

You can always do this:

if condition
render :page and return

How can you do this:

if condition
redirect_to and return
+1
source

Source: https://habr.com/ru/post/1794303/


All Articles