Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Way to see which rails controller/model is serving the page?

This might be a slightly odd question, but I was wondering if anyone know a Rails shortcut/system variable or something which would allow me to keep track of which controller is serving a page and which model is called by that controller. Obviously I am building the app so I know, but I wanted to make a more general plugin that would able to get this data retroactively without manually going through it.

Is there any simple shortcut for this?

like image 962
Paul Kaplan Avatar asked May 24 '11 15:05

Paul Kaplan


2 Answers

The controller and action are defined in params as params[:controller] and params[:action] but there is no placeholder for "the model" as a controller method may create many instances of models.

You may want to create some kind of helper method to assist if you want:

def request_controller
  params[:controller]
end

def request_action
  params[:action]
end

def request_model
  @request_model
end

def request_model=(value)
  @request_model = value
end

You would have to explicitly set the model when you load it when servicing a request:

@user = User.find(params[:id])

self.request_model = @user
like image 138
tadman Avatar answered Sep 28 '22 19:09

tadman


There are a number of ways that I know of:

First you can do rake routes and check out the list of routes.

Second you could put <%= "#{controller_name}/#{action_name}" %> in your application.html.erb and look at the view to see what it says. if you put it at the extreme bottom you'll always get that information at the bottom of the page.

like image 40
jaydel Avatar answered Sep 28 '22 17:09

jaydel