views:

50

answers:

2

I have an action in my RoR application, and it calls a different script depending on the user running it.

 def index

 @user = User.find(session[:user_id], :include => [ :balances, :links, :comments ])

 render :file => "#{RAILS_ROOT}/app/views/user/index_#{@user.class.to_s.downcase}.html.erb"

  end

How to make the call to render a more elegant and simple?

+1  A: 

You can make it a partial (index_whatever.html.erb --> _index_whatever.html.erb) and it would look like this:

def index
  @user = User.find(session[:user_id], :include => [ :balances, :links, :comments ])
  render :partial => "index_#{@user.class.to_s.downcase}"
end

Also, what I would do is add a method in the user model, like this:

def view
  "index_#{class.to_s.downcase}"
end

So your index action would be:

def index
  @user = User.find(session[:user_id], :include => [ :balances, :links, :comments ])
  render :partial => @user.view
end
jordinl
Thank you jordinl.
Javier Valencia
+2  A: 

Try:

render :template => "user/index_%s" % @user.class.to_s.downcase
Chris Heald
Thank you Chris.
Javier Valencia