Different set of Views for different user's roles

て烟熏妆下的殇ゞ 提交于 2019-12-01 07:52:59

问题


I am developing a rails app and I have 2 different user's role: advanced and basic.

Instead of to hide links in the basic user's views (a.i. using CanCan ) I want to manage 2 different set of views: one for the advanced user and one for basic user.

Currently I am working in this way:

 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 dont like to specify at every action the template for the basic user ( { render :template => "devices/index_basc.html.erb" } ) I think there is some other way (I hope more neat :)

Do you have any ideas ?

Thank you, Alessandro


回答1:


You can do something like in this Railscast Mobile Devices:

in config/initializers/mime_types.rb add:

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

in app/controllers/application_controller.rb add:

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

Now you can just do the following in your controllers:

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



回答2:


As you have only two different user's role you can do this

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


来源:https://stackoverflow.com/questions/3157063/different-set-of-views-for-different-users-roles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!