All threads / Persist file info after rails validation error

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Persist file info after rails validation error

Alex Deering asked in General

Im working on my main form and have some form validations and running into an issue with a new form not maintaining the image upload after a validation error reloads the page. Here is my form-group for the image upload:

<div class="form-group">
          <%= form.label :image, "Recipe Photo" %>
          <div class="flex flex-row">
            <div class="w-full">
              <% if form.object.image.attached? %>
                <%= image_tag form.object.image, class: "w-full border rounded-lg self-center" %>
              <% end %>  
            </div>
          </div>
          <%= form.file_field :image, direct_upload: true, class: "form-control" %>
        </div>

Because I'm using direct upload the image gets uploaded and still exists after the reload and displays the image with the image_tag in the above code. But when the form is resubmitted the connection in the active_storage tables isnt maintained. Is there a hidden field or something i can put to ensure it gets sent as a param the next time the form is submitted?

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 34,674+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.