Quesofat
Quesofat

Reputation: 1531

Undefined method when passing variable into erb

I am trying to pass json response into my index view in erb.

In ERB I am trying to check if the value is true, render certain html, if it is false, render some html.

Routes:

require 'sinatra'
require 'httparty'
require 'erb'


get '/' do
  headers = {
    "X-Auth-Email" => 'null',
    "X-Auth-Key" =>   'null',
    "Content-Type" => 'application/json'
  }

  isDevModeOn = HTTParty.get(
    'https://api.cloudflare.com/client/v4/zones/null/settings/development_mode',
    :headers => headers
  )

  erb :index, :locals => isDevModeOn
end #end for get

View

<html>
  <head>
    <title>CloudFlare App</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <link rel="stylesheet" href="css/master.css">
  </head>
  <body class="container-fluid">
    <div class="row">
      <div class="col-md-3">
        <div style="border: solid 1px black; height: 400px; width: 400px;" class="tile">
          <h2>Dev Mode Status</h2>

          <% if isDevModeOn.result.value? %>
            <span style="background: green;">ON</span>

            <a href="#">Turn off Dev Mode</a>
          <% else %>
            <span style="background: red;">OFF</span>

            <a href="#">Turn On Dev Mode</a>
          <% end %>
        </div>
      </div>
    </div>
  </body>
</html>

response example

{
  "success": true,
  "errors": [],
  "messages": [],
  "result": {
    "id": "development_mode",
    "value": "off",
    "editable": true,
    "modified_on": "2014-01-01T05:20:00.12345Z",
    "time_remaining": 3600
  }
}

Disclaimer: I have a javascript background and use EJS a lot and am pretty inexperienced with ruby.

Upvotes: 0

Views: 64

Answers (1)

whodini9
whodini9

Reputation: 1434

Pass isDevModeOn as a param:

require 'sinatra'
require 'httparty'
require 'erb'


get '/' do
  headers = {
    "X-Auth-Email" => 'null',
    "X-Auth-Key" =>   'null',
    "Content-Type" => 'application/json'
  }

  isDevModeOn = HTTParty.get(
    'https://api.cloudflare.com/client/v4/zones/null/settings/development_mode',
    :headers => headers
  )

  erb :index, :locals => {:isDevModeOn => params[:isDevModeOn]}
end #end for get

Upvotes: 1

Related Questions