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. Obviousl开发者_如何学编程y 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?
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
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.
The controller can be accessed through the params
hash: params[:controller]
. There isn't really a way to get the model used by a controller though, because there is no necessary correlation between any controller and any model. If you have an instance of the model, you could check object.class
to get the model's class name.
精彩评论