How access controller method in models | Ram Laxman Yadav

How access controller method in models

How ever in practice some time we require to access the controller’s methods in the models.It’s quite easy to do so in rails. Here is an explanation of that:

Add a before_filter method inside the Application Controller

Add a before_filter method inside the application controller and write the following command code inside that function

Thread.current.thread_variable_set('controller', self)

Access inside the model

Now you can access the controller’s method inside the model by using

Thread.current.thread_variable_get('controller').method_name

Note

You have to make your method_name public to avoid it calling from browser as action have to hide it by

hide_action :method_name

Ram Laxman Yadav
Ram Laxman Yadav
Ruby on Rails Developer