views:

49

answers:

2

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

A: 

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}
Salil
But he still needs to do this in every action. He said: "But I dont like to specify at every action the template for the basic user ( { render :template => "devices/index_basc.html.erb" } )"
jigfox
+3  A: 

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
jigfox
index.basic.erb? which file format is this
Salil
`Mime::Type.register_alias "text/html", :basic` it's an alias for html, but this way you can have two different templates depending on the type of user
jigfox
What is the downvote for? It would be nice to know what I've done wrong to correct it.
jigfox
I think it is a good hack, but I don't know if this is it a "best practice" because MIME is usually used for different purpose. (I voted up your response)
the mime type sent to the browser is still `text/html`, it's just a rails internal alias.
jigfox