Rails require external ruby file containing configuration variables at runtime (variable name)-Collection of common programming errors
When exporting my data to Excel, I prompt the user to select his export template.
Each export template should be a separate (ruby) file that contains several parameters.
I tried to “require” my file after selects it from a drop-down list but the variables that the ruby file contains are not accessible.
What is the best solution to include a ruby file at runtime, depending on the previous user’s choice ?
The goal is to include different set of parameters at runtime
My code :
Class ExportController < ApplicationController
...
def step3
    filepath = params[:template][:filepath]
    if File.exists?(file=File.join(Rails.root, filepath))
        logger.debug ("-----------> File FOUND : "+file.to_s)    
                     # This gives:  File FOUND : D:/Rails/test1/lib/export/test2.rb
        require file
    else
        logger.debug ("-----------> File not found !!")
    end
end
File test2.rb :
@test = "Hello world"
In the view :
gives nothing… 🙁
- 
I thing that what you need to do is call eval on the file’s content.
It’s not a very good idea though… It evaluates ruby code runtime.
Ex (in a rails console):
>eval('@test = 450') > 450 >@test > 450 - 
I am not sure this would be going to resolve your problem. But as per What I understand is you need to generate template dynamically for your excel export. I suggest you to have a look of ERB templating. In this you will find how you can generate templates dynamically then you can render them in rails.
Hope I have understood correctly and this answer will help you.
 - 
Finally, I chose to import a configuration file coded in YAML. This seems the best way to import variables depending on a user’s choice (load a different yaml file at runtime)