views:

110

answers:

1

I have a couple different user types (buyers, sellers, admins).

I'd like them all to have the same account_path URL, but to use a different action and view.

I'm trying something like this...

class AccountsController < ApplicationController
  before_filter :render_by_user, :only => [:show]

  def show
   # see *_show below
  end

  def admin_show
    ...
  end

  def buyer_show
    ...
  end

  def client_show
    ...
  end
end

This is how I defined render_by_user in ApplicationController...

  def render_by_user
    action = "#{current_user.class.to_s.downcase}_#{action_name}"
    if self.respond_to?(action) 
      instance_variable_set("@#{current_user.class.to_s.downcase}", current_user) # e.g. set @model to current_user
      self.send(action)
    else
      flash[:error] ||= "You're not authorized to do that."
      redirect_to root_path
    end
  end

It calls the correct *_show method in the controller. But still tries to render "show.html.erb" and doesn't look for the correct template I have in there named "admin_show.html.erb" "buyer_show.html.erb" etc.

I know I can just manually call render "admin_show" in each action but I thought there might be a cleaner way to do this all in the before filter.

Or has anyone else seen a plugin or more elegant way to break up actions & views by user type? Thanks!

Btw, I'm using Rails 3 (in case it makes a difference).

+1  A: 

Depending on how different the view templates are, it might be beneficial to move some of this logic into the show template instead and do the switching there:

<% if current_user.is_a? Admin %>
<h1> Show Admin Stuff! </h1>
<% end %>

But to answer your question, you need to specify which template to render. This should work if you set up your controller's @action_name. You could do this in your render_by_user method instead of using a local action variable:

def render_by_user
  self.action_name = "#{current_user.class.to_s.downcase}_#{self.action_name}"
  if self.respond_to?(self.action_name) 
    instance_variable_set("@#{current_user.class.to_s.downcase}", current_user) # e.g. set @model to current_user
    self.send(self.action_name)
  else
    flash[:error] ||= "You're not authorized to do that."
    redirect_to root_path
  end
end
Andrew Vit
Yep - it makes sense, but was hoping there was a way to do this entirely in the `render_by_user` before filter. Thanks for the post!
Brian Armstrong
@Brian, updated advice for you: see above!
Andrew Vit
oh cool! Didn't know you could set the action_name like that. Yep, this looks like a winner. Will try it out!
Brian Armstrong