Search code examples
htmlruby-on-railsrails-activestorage

Active Storage is outputting image metadata in HTML


I am creating a form where a user can change the images they uploaded. The problem is the form to edit the images is displaying the image metadata along with the images that have been uploaded. Alongside the images, in the HTML page it outputs the following:

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

How do I prevent it from displaying the above data?

Here is the relevant code html.erb file that displays the images

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

Model

class Space < ApplicationRecord
  belongs_to :user

  has_many_attached :image

end

Controller

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

Solution

  • On:

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

    Remove the = after the <%.

    On an ERB tag, the equals sign will print the result of that line. It is printing the .inspect on the image object.