0


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..

1 Answers1

0

If params method won't help you to achieve your goal (but it's worth to try) you can always access current instance of controller object by calling self in context of any instance method or action.

To test you can put byebug in any action, call that action in browser with additional parameters and type self in console.

For example, in controller:

class UsersController < ApplicationController
  def show
    byebug
  end
end

in browser:

localhost:3000/?some_param=1234&another_param=testing

There will be a lot of useful stuff in there, like self.instance_variables => [.... :@_request, ... :@_params].

Also request method contain all info about current request including parameters.

Hope that'll help.

Vladv
  • 26
  • 4
  • Thanks @Vladv for the answer. Using "self" inside application controller is now returning me the Controller Object . But now, I have to get the "entity_params" private method inside "entities_controller". When I try to print this method, it throws the error "param is missing or the value is empty: Entities". If I define a new method to return the private method, it throws "private method `testeParams' called for #" Any idea how can/should I get these parameters? – Chimmichunga Oct 30 '17 at 14:05