开发者

Different set of Views for different user's roles

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


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


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}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜