Nital
Nital

Reputation: 6084

How can I display all the environment variables on an HTML page using Rails?

I have a bunch of environment variables set on my Windows machine that I would like to display on an HTML page. I am able to successfully display just one but not sure how to loop and display all of them with key-value pairs.

Please guide.

Rails version: 4.2

Environment Variables:

MY_ENCODING_SCHEME: utf8
DB_CONN_POOL: 10
DB_USER_NAME: guest
DB_PWD: secret

index.html.erb

<div class="table-container">
  <table class="table">
    <thead>
    <tr>
      <th colspan="2">Environment Variables</th>
    </tr>
    </thead>
    <tbody>
        <tr><td><%= ENV["MY_ENCODING_SCHEME"] %></td></tr>
    </tbody>
  </table>

Upvotes: 0

Views: 4717

Answers (2)

vthomas2007
vthomas2007

Reputation: 153

You could always do something like this:

<% ENV.each do |k, v| %>
    <tr>
        <td>
            <%= "#{k} - #{v}" %>
        </td>
    </tr>
<% end %>

Is that what you had in mind?

EDIT: Just be sure this doesn't see the light of day on a production environment!

Upvotes: 1

sugaryourcoffee
sugaryourcoffee

Reputation: 879

You could loop through your environment variables like this

<table>
  <thead><tr><td>Variable</td><td>Value</td></tr></thead>
  <tbody>
    <% ENV.each do |k,v| %>
      <tr><td><%= k %></td><td><%= v %></td></tr>
    <% end %>
  </tbody>
</table>

Upvotes: 1

Related Questions