0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[ActiveStorage]画像複数枚投稿のエラー

0
Last updated at Posted at 2021-05-15

※プログラミング学習中の私がアップロードしてます。理解が浅いです。
この記事は厳密な仕様に関するものではなく、考え方理解のまとめとして受け取ってください。

現在、フリーマーケットアプリの開発中です。

環境

Rails 6.0.3.6
Activestorage 6.0.3.6
Ruby 2.6.5p114
Devise 4.7.3

現在写真投稿機能にプレビュー機能を追加が終わり、続いて複数枚投稿機能を実装中です。
写真に関する複数枚表示を実装しております。

エラー

20210515-170526.png

こちらのエラーについては同じく受講生さんの@zuu_aaさんの投稿で解決出来そうだと考えたのですが、

これだと1枚目(images[0])に限定するので悩んでいました。
結果は以下
20210515-171546.png

もう一度ブロック変数で取れている中身を確認し見ると

<%= link_to item_path(item.id) do %>
 <div class='item-img-content'>
   <% item.images.each do |i| %>
   <%= image_tag item.images[0], class: "item-img" %>
   <% if item.purchase.present? %>
>> i
=> #<ActiveStorage::Attachment id: 41, name: "images", record_type: "Item", record_id: 39, blob_id: 41, created_at: "2021-05-15 07:38:54">

>>  item.images
=> #<ActiveStorage::Attached::Many:0x00007fccdaa34fb8 @name="images", @record=#<Item id: 39, item_name: "www", description: "www", category_id: 3, status_id: 2, delivery_fee_payment_id: 3, prefecture_id: 2, delivery_prepare_id: 2, price: 3400, user_id: 2, created_at: "2021-05-15 07:38:54", updated_at: "2021-05-15 07:38:54">>

というふうに取得できているので

以下のように書き換えるとうまくいきました。

    <% @item.images.each do |i| %>
      <%= image_tag i, class:"item-box-img" %>
      <%# 商品購入後実装 %>
      <% if @item.purchase.present?%>
      <div class="sold-out">
        <span>Sold Out!!</span>
      </div>
      <% end %>
    <% end %>

結果
20210515-172054.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?