首页 > 解决方案 > Active Storage 以 HTML 格式输出图像元数据

问题描述

我正在创建一个表单,用户可以在其中更改他们上传的图像。问题是编辑图像的表单显示图像元数据以及已上传的图像。除了图像之外,它还在 HTML 页面中输出以下内容:

[#<ActiveStorage::Attachment id: 3, name: "image", record_type: "Space", record_id: 1, blob_id: 3, created_at: "2018-08-20 00:52:57">, 

如何防止它显示上述数据?

这是显示图像的相关代码 html.erb 文件

<div>
  <% if @space.image.attached? %>
      <%= @space.image.each do |image| %>
      <%= image_tag image  %>
    <% end %>
  <% end %>
</div>

模型

class Space < ApplicationRecord
  belongs_to :user

  has_many_attached :image

end

控制器

class SpacesController < ApplicationController
  before_action :authenticate_user!, except: [:show]

  def index
    @spaces = current_user.spaces
  end

  def new
    @space = current_user.spaces.build
  end

  def create
    @space = current_user.spaces.build(space_params)
    if @space.save
      redirect_to listing_space_path(@space), notice: "Saved..."
    else
      flash[:alert] = "Something went wrong..."
      render :new
    end
  end

  def show
  end

  def update
    if @space.update(space_params)
      flash[:notice] = "Saved!"
    else
      flash[:notice] = "Something went wrong. Please check your submission and try again."
    end
      redirect_back(fallback_location: request.referer)
  end

  private
    def space_params
        params.require(:space).permit(:name, :description, image: [])
    end

end

标签: htmlruby-on-railsrails-activestorage

解决方案


上:

<%= @space.image.each do |image| %>

去掉=后面的<%

在 ERB 标签上,等号将打印该行的结果。它正在.inspect图像对象上打印。


推荐阅读