Topic: How do I know which controller I'm in?

I have an rhtml file that is used by two controllers, how do I determine which controller is currently rendering the file?

Re: How do I know which controller I'm in?

in your controller:

  @current_controller = controller_name
#and i dont mean, write the controller name.. "controller_name" is a function built into rails.
#cheers

in your view:
  if @current_controller == 'controller1'
    #do something
  else #other controller.
    #do something else
  end

Mileage may vary.

Last edited by pjleonhardt (2006-10-15 01:10:11)

Re: How do I know which controller I'm in?

There's also params[:controller]

Railscasts - Free Ruby on Rails Screencasts

Re: How do I know which controller I'm in?

How about identifying the (child?) controller in this instance?
eg http://localhost:3000/items/list

controllers/items_controller.rb:

class ItemsController < AdminController
end

controllers/admin_controller.rb:
class AdminController < ApplicationController
  def list
    case controller_name # << how to get this to say "items" not "admin"
      when "items"
        ...
    end
  end
end

Last edited by zubin8or (2006-11-09 05:21:53)

Zubin

Re: How do I know which controller I'm in?

I'm not certain I understand. How is the admin controller related to the Items controller?

Edit: Oy, didn't notice ItemsController was a subclass of AdminController. Nevermind.

Last edited by ryanb (2006-11-09 18:25:21)

Railscasts - Free Ruby on Rails Screencasts

Re: How do I know which controller I'm in?

just do this:

class ItemsController < AdminController
  def controller_name
    'ItemsController'
  end
end

Though Ryan's suggestion may suite your needs.