0

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.

6
  • Do you have a controller set up to handle this request? It's not good practice to put all that code in a route file like that. That's supposed to be handled by a controller method. Commented Apr 4, 2017 at 18:22
  • No I don't. Not sure what a controller would look like. In node, I usually dump my requests in the route so I didn't see an issue with it here. The file I provided would be the equivalent of my app.js in node. Commented Apr 4, 2017 at 18:24
  • In Sinatra what you're talking about would be fine, but I'm unfamiliar with most Sinatra. Do you have a Rails or Sinatra app? Commented Apr 4, 2017 at 18:32
  • I'm using sinatra. Rails seemed to be overkill here Commented Apr 4, 2017 at 18:33
  • In that case I can't be much help, sorry. Saw the Rails tag and assumed a rails question. Commented Apr 4, 2017 at 18:34

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

That seemed to help me get past that error. I have undefined method `result' for nil:NilClass as an error now. Is dot notation still accepted in erb?
What happens if you try to display just the params? I'm not really familiar with HTTParty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.