我正在尝试将json响应传递到erb中的索引视图中。
在ERB中我试图检查值是否为true,渲染某些html,如果为false,则渲染一些html。
路线:
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
查看
<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>
响应示例
{
"success": true,
"errors": [],
"messages": [],
"result": {
"id": "development_mode",
"value": "off",
"editable": true,
"modified_on": "2014-01-01T05:20:00.12345Z",
"time_remaining": 3600
}
}
免责声明:我有一个javascript背景并且经常使用EJS而且对ruby非常缺乏经验。
答案 0 :(得分:1)
将isDevModeOn
作为参数传递:
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