A different set of views for different user roles

I am developing a rails application, and I have 2 different user roles : advanced and basic.

Instead of hiding the links in the main user views (ai using CanCan), I want to manage 2 different sets of views : one for the advanced user and one for the main user.

I am currently working as follows:

 case current_operator.op_type
      when 'basic'
        format.html { render :template => "devices/index_basc.html.erb" }
      when 'advanced'
        format.html # index.html.erb
 end

But I do not want to specify a template for the main user with every action ({render: template => "devices / index_basc.html.erb"}) I think there is another way (I hope more neat :)

Do you have any ideas?

Thanks Alessandro

+3
2

- Railscast :

config/initializers/mime_types.rb :

Mime::Type.register_alias "text/html", :basic 

app/controllers/application_controller.rb :

before_filter :check_user_status
private
def check_user_status
  request.format = :basic if current_operator.op_type == 'basic'
end

:

class SomeController < ApplicationController
  def index
    # …
    respond_to do |format|
      format.html  # index.html.erb
      format.basic # index.basic.erb
    end
  end
end
+7

,

page = (current_operator.op_type =='basic')?  "devices/index_basc.html.erb"  : "index.html.erb"
format.html { render :template => page}
0

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


All Articles