ruby-on-rails - Rails 无意中在 View 中显示 JSON

标签 ruby-on-rails json

我正在使用 Rails 4,并且在我的一个 View 中遇到了一个奇怪的问题。在我的 Controller 中,我使用 Ruby_Open_Weather_Map 从 OpenWeatherMap.org 获取天气信息。 gem 。数据以 JSON 形式返回,我循环遍历 JSON 并提取 future 5 天的预报信息。奇怪的是,在我的循环之后,显示的是 JSON 数据。

enter image description here

我是 Rails 和 JSON 的新手,这对我来说没有任何意义。我有第二页,上面有更多信息,同样的事情发生了。这是我的 Controller 的样子:

  def index
    options = { units: "Imperial", APPID: '6eff99d4b460d3a1acddcf1a727a7a45' }
    @currentweather = OpenWeather::Current.city("Greenville, SC", options)
    @forcastweather = OpenWeather::ForecastDaily.city("Greenville, SC", options)
  end

这就是我的观点:

    <%= @forcastweather['list'].each do | day | %>
    <div class ="row">
      <div class="col-xs-5">
        <strong><%= Time.at(DateTime.strptime(day['dt'].to_s,'%s')).in_time_zone("Eastern Time (US & Canada)").strftime("%A") %></strong>
      </div>
      <div class="col-xs-3">
        <%= day['temp']['min'] %>
      </div>
      <div class="col-xs-3">
        <%= day['temp']['max'] %>
      </div>
      <div class="col-xs-2">
        <img src="http://openweathermap.org/img/w/<%= day['weather'][0]['icon'] %>.png" alt="<%= day['weather'][0]['main'].humanize %>">
      </div>
    </div>
  <% end %>

为什么 JSON 显示在我的 View 中,我该如何解决?

最佳答案

<%= @forcastweather['list'].each do | day | %>

把=去掉

<% @forcastweather['list'].each do | day | %>

使用 <%=将告诉 rails 评估此代码,然后呈现结果,因此 rails 评估代码,循环条目,写入 html...然后呈现 @forcastweather['list'] 的值.

<%将告诉 Rails 评估代码,但不从中渲染任何内容。

关于ruby-on-rails - Rails 无意中在 View 中显示 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36334859/

相关文章:

ruby-on-rails - Ruby on Rails 安装 Ubuntu 14.04 LTS---gem install rails 错误

ruby-on-rails - 限制 Rails 应用程序生成的出站 API 调用

java - Http Post的响应代码是415。如何使响应代码为200

java - 对象与 JSON 之间的转换

ruby-on-rails - Dockerizing 延迟的作业

ruby-on-rails - Active Record 按最小值排序

python - 使用 jsonpickle 从文件中保存和加载对象

javascript - 由于页面导航离开而取消 AJAX POST 请求

ruby-on-rails - 用户的未定义方法 attr_accessible 错误

php - Symfony 4 - JSON 身份验证不起作用