たれみみマンデー

モデルのモデルのモデルをネストして画像も同時にフォームで保存

ポイントはfields_forとobjectで画像にアクセス。

プレビュー出すのアクセスかなりハマった。

<div class="image-upload-container">
     <%= f.fields_for :thumbnails do |t| %>
        <div class="image-upload-box">
    
        <%= t.file_field :image, class: "btn-upload" %>
        <% if  t.object.image.url.present? && n>=3 %>
         <%= t.check_box :_destroy , class: "check-destroy" %>
          <%= t.label :_destroy , class: "btn-destroy" do %>
          <% end %>
        <% end %>
   
        <%= t.label :image, class: "btn-upload-label" do %>
   
          <% if t.object.image.url.present? %>                                                         
            <%= image_tag t.object.image.url, class:"uploaded-image" %>
          <% end %>
          <span class="label-plus">+</span>
          <span class="label-text">画像未選択</span>
   
        <% end %>
   
        <% n+=1 %>
      </div>
    <% end %>
  </div>

 

自分用ビボウロク。