Skip to content

Instantly share code, notes, and snippets.

@Mk-Etlinger
Created February 22, 2018 06:11
Show Gist options
  • Save Mk-Etlinger/9a17dea7005f695df6493f37823d305c to your computer and use it in GitHub Desktop.
Save Mk-Etlinger/9a17dea7005f695df6493f37823d305c to your computer and use it in GitHub Desktop.
Metaprogram setting your resource as an instance variable
class ApplicationController < ActionController::API
before_action :authenticate_user
def sorcery
controller_name = params['controller']
controller_name.chop!
model_name = controller_name.capitalize
instance_variable_set('@' + controller_name, Object.const_get( model_name ).find_by(id: params[:id]))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment