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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With