mardi 8 décembre 2015

Rails: share params between controllers

I have a rails 3 app and I'm developing a bot over it.

The basic idea is that an user will be able to communicate with the app using its favourite messaging service. The bot will then evaluate the received message and direct the message to the proper controller and action. Please keep in mind that this shouldn't be done in the routing level since the evaluation requires to execute queries, some logic etc.

Although I know is possible to send params to another Controller and action simply by passing variable to the respective methods ( refactoring the method with default values for the variables --something like def hello( var_a = params['one'] , var_b = params['tow']...) I wouldn't like to go this way since it would require to refactor a lot of written code.

In this context, I was thinking it its possible to create an initialize method in the controller I will need to pass the params. Something like:

ExistingController < ActiveRecord::Base

def self.start_with_external_params ( params )
    # some code here 
end

And then use the passed params as usually in the controller -- by calling ExistingController.new.start_with_external_params(params).some_method but I couldn't figure out how to make it, can someone give me some advice on this matter?

Aucun commentaire:

Enregistrer un commentaire