0
votes

I've followed the rails ActiveStorage guide. I've successfully saved the image to the blob table in the local database (the attachment table is empty). But I can't seem to show it in the view:

<%= url_for(@comment.images) if @comment.images %>

does not work nor does:

     <% @comments.each do |c| %>
        <%= c.body %>

        <%= url_for(c.images) if c.images %>

      <% end %>

Showing other values of comment works, so this is specific to the images. I've also tried adding .url to "images." And I've tried image_tag instead of url_for. No luck in both cases. I'd really appreciate any help.

And my model

  has_many_attached :images

application.js

//= require activestorage


import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"


Rails.start()
Turbolinks.start()
ActiveStorage.start()

controller

    private
        def comment_params
            params.require(:comment).permit(:body, images: [])
    end
1
Is images really plural? If so you will need to loop through them.Rockwell Rice
@RockwellRice Thank you for the comment. I've tried <% c.images.each do |i| %> <%= url_for(i) %> <% end %> But no use. Also tried image_tag and without any, just "i"amazingbot
Please add more details to the question. Add the model and how you have setup the attachmentDeepak Mahakale
@amazingbot That is fine, but if it is plural then you do need to loop through it no matter what, your solution comes inside a loop.Rockwell Rice
@DeepakMahakale Thank you for the comment. I've updated the question.amazingbot

1 Answers

0
votes

You need to use activestorage representation method. Try this:

<% @comments.each do |c| %>
  <%= c.body %>
  <% c.images.each do |image| %>
    <%= image_tag image.representation(resize: '500x500') %>
  <% end %>
    
<% end %>