1
votes

I've followed the Rails Active Storage guide (https://guides.rubyonrails.org/active_storage_overview.html#direct-upload-javascript-events). But when I upload an image and click post, the text is saved while the image is not. I've checked all three tables (active_storage_attachments, etc.) and they are all empty. What could I be doing wrong?

view

            <%= form_for([@book, @comment]) do |form| %>
                <div>
                  <%= form.text_area :body, class:'', placeholder: 'name...'%>
                </div>

               <%= form.file_field :image, multiple: true, direct_upload: true %>

          <div class="">
          <%= form.submit "Done", class:''%> 
          </div>

application.js (the guide says to include activestorage.js in the javascript bundle, but I wasn't sure where that was in Rails 6. Also the code required seems to be already in this application.js, perhaps when I ran active storage install)

//= 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()

model

  has_many_attached :images

gemfile

gem "aws-sdk-s3", require: false

development.rb

  config.active_storage.service = :local

1
Try adding multipart: true to the form - arieljuod
@arieljuod OK, that seems to have solved it. Now I'm trying to show it, which isn't working (neither <%= image_tag(@comment.images) %> nor <%= url_for(@comment.images) %> works. For that I'll create another question if I can't resolve it on my onw. - amazingbot
@arieljuod If you'd like to post your comment as an answer I'd be happy to accept it - amazingbot
@arieljuod Just to make sure, the image is saved into blob table and not into attachments table. Is that normal? - amazingbot

1 Answers

0
votes

You have has_many_attached :images in the model

But in the form is form.file_field :image, multiple: true

You need to change input name to form.file_field :images, multiple: true