开发者

Rails way to render different actions & views based on user type?

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).


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
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜