A way to see which controller / rail model serves the page?

This may be a bit of a strange question, but I was wondering if anyone knew about the Rails shortcut / system variable or something that would allow me to keep track of which controller serves the page and which model is being called by that controller. Obviously, I am creating an application, so I know, but I wanted to create a more general plugin that could receive this data retroactively without having to go through it manually.

Is there a simple shortcut for this?

+7
source share
3 answers

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

You might want to create some kind of helper method that will help you:

 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 will need to explicitly install the model when it is loaded when serving the request:

 @user = User.find(params[:id]) self.request_model = @user 
+8
source

There are several ways that I know:

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

Secondly, you can 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 bottom, you will always get this information at the bottom of the page.

+6
source

Access to the controller can be obtained through the hash params : params[:controller] . There really is no way to get the model used by the controller, because there is no necessary correlation between any controller and any model. If you have a model instance, you can check object.class to get the model class name.

+1
source

All Articles