Hello people.
I'm creating a log process in my Rails 5 application, inside the application controller. I'm creating there because I want to call the log process inside many controllers with a before_save property. The log will save the changes that user performs in the form on edit view template. The problem is that I can't get the <ObjectController:> inside application controller. I've already got the instance variable from the controller, but I need the ObjectController too, because I have to get the strong parameters from controller object. The strong parameters holds all data that user inserted on input fields.
This is what I've done already:
app/controllers/application controller
def log
@controlr = instance_variable_get("@#{controller_name.singularize}") #get the edited object
attribs = @controlr.attribute_names #get object table column names
edited_data = controlr_params #stuck here!
ctrlr = @controlr.attributes #retrive object data from db
...
##compare the edited_data with the actual data from db and check if something was changed
end
So, I need to obtain the Controller Object to access the strong parameters in order to compare if user edited any data. I'm not sure if this is the best way/practice to do this. If there is a better way, I'd like to know. But I need to call this process in a great number of controllers that require a data log.
Thanks for you time and sorry any bad english..
If
paramsmethod won't help you to achieve your goal (but it's worth to try) you can always access current instance of controller object by callingselfin context of any instance method or action.To test you can put
byebugin any action, call that action in browser with additional parameters and typeselfin console.For example, in controller:
in browser:
There will be a lot of useful stuff in there, like
self.instance_variables=>[.... :@_request, ... :@_params].Also
requestmethod contain all info about current request including parameters.Hope that'll help.