【问题标题】:Turn image URL into the image after parsing JSon using Rails使用Rails解析JSon后将图片URL转为图片
【发布时间】:2018-11-07 21:08:18
【问题描述】:

我的问题是,当我使用 HTTParty on Rails 解析 JSON 时,如何呈现图像本身而不是图像 url。我能够在它通过时渲染 image_url 但想要那里的图像吗?例如,而不是看到: "image_url":"https://d2eyrv63e6x6lp.cloudfront.net/wp-content/uploads/2015/02/20141833/John-Smith.jpg"

我想看看它所代表的实际图像。

我正在使用 HTTParty 在 Ruby on Rails 5 上解析 JSON。

这是我目前的看法:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Image</th>
      <th>Title</th>
      <th>Bio</th>
      <th>Vote</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @rosters.each do |roster| %>
      <tr>
        <td><%= roster['name'] %></td>
        <td><%= roster['image_url'] %></td>
        <td><%= roster['title'] %></td>
        <td><%= roster['bio'] %></td>
        <td><%= roster['vote'] %></td>
      </tr>
    <% end %>
  </tbody>
</table>

当前控制器:

def index
    @rosters = HTTParty.get('https://api.example', :headers =>{'Content_Type' => 'application/json'})
end

当前型号:

class Roster < ApplicationRecord

include HTTParty
end

【问题讨论】:

    标签: json ruby ruby-on-rails-5


    【解决方案1】:

    您正在寻找的是image_tag 助手

    所以在你的情况下:

    <td><%= image_tag(roster['image_url']) %></td>
    

    应该做的伎俩

    【讨论】:

    • 我们如何添加一个类来调整图像大小?
    • @Sohel 您是否点击了提供的链接?它对这个主题的解释性很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-23
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2017-02-11
    • 2016-04-16
    • 1970-01-01
    相关资源
    最近更新 更多