{"id":2493,"date":"2022-08-30T15:25:19","date_gmt":"2022-08-30T15:25:19","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/27\/rails-require-external-ruby-file-containing-configuration-variables-at-runtime-variable-name-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:25:19","modified_gmt":"2022-08-30T15:25:19","slug":"rails-require-external-ruby-file-containing-configuration-variables-at-runtime-variable-name-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/rails-require-external-ruby-file-containing-configuration-variables-at-runtime-variable-name-collection-of-common-programming-errors\/","title":{"rendered":"Rails require external ruby file containing configuration variables at runtime (variable name)-Collection of common programming errors"},"content":{"rendered":"<p>When exporting my data to Excel, I prompt the user to select his export template.<\/p>\n<p>Each export template <strong>should be a separate (ruby) file<\/strong> that contains several parameters.<\/p>\n<p>I tried to &#8220;require&#8221; my file after selects it from a drop-down list but the variables that the ruby file contains are not accessible.<\/p>\n<p><strong>What is the best solution to include a ruby file at runtime, depending on the previous user&#8217;s choice ?<\/strong><\/p>\n<p>The goal is to include different set of parameters at runtime<\/p>\n<p>My code :<\/p>\n<pre><code>Class ExportController &lt; ApplicationController\n...\ndef step3\n    filepath = params[:template][:filepath]\n    if File.exists?(file=File.join(Rails.root, filepath))\n        logger.debug (\"-----------&gt; File FOUND : \"+file.to_s)    \n                     # This gives:  File FOUND : D:\/Rails\/test1\/lib\/export\/test2.rb\n        require file\n    else\n        logger.debug (\"-----------&gt; File not found !!\")\n    end\n\nend\n<\/code><\/pre>\n<p>File test2.rb :<\/p>\n<pre><code>@test = \"Hello world\"\n<\/code><\/pre>\n<p>In the view :<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>gives nothing&#8230; \ud83d\ude41<\/p>\n<ol>\n<li>\n<p>I thing that what you need to do is call eval on the file&#8217;s content.<\/p>\n<p>It&#8217;s not a very good idea though&#8230; It evaluates ruby code runtime.<\/p>\n<p>Ex (in a rails console):<\/p>\n<pre><code>&gt;eval('@test = 450')\n&gt;  450\n&gt;@test\n&gt;  450\n<\/code><\/pre>\n<\/li>\n<li>\n<p>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.<\/p>\n<p>Hope I have understood correctly and this answer will help you.<\/p>\n<\/li>\n<li>\n<p>Finally, I chose to import a configuration file coded in YAML. This seems the best way to import variables depending on a user&#8217;s choice (load a different yaml file at runtime)<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-27 02:21:14. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>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 &#8220;require&#8221; my file after selects it from a drop-down list but the variables that the ruby file contains are not accessible. What is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2493","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=2493"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2493\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}