I think there are a few things you can do. If you do not need to respond in a specific format, you can simply redirect the request with an appropriately formatted parameter. For instance:
class TwilioController def create if params[:twilio_action] == 'create group' redirect_to create_group_path(:id => params[:group_id], :number => params[:number]) end end end
There is a good chance that you will have authentication problems, because twilio api will not send and receive cookies for you, so you will not have an authenticated user. If so, it is best to put all of your common code in the model and handle cookie authentication using your GroupController check and phone number using TwilioController. For instance:
class TwilioController def create if params[:twilio_action] == 'create group' if can_create_group?(params[:phone_number]) Group.create(:id => params[:group_id]) end end end end
It is always better to put your business logic in your model, but if you really have a function that you want to share in two controllers, you can always create a module for this:
module GroupControllerActions def create_group user Group.create(params[:group].merge({:user => user})) end end class TwilioController include GroupControllerActions def create if params[:twilio_action] == 'create group' create_group(User.find_by_number(params[:phone_number])) end end end class GroupsController def create create_group(current_user) end end
source share