DEV Community

Discussion on: Handling ActiveStorage direct uploads and server side form validations

Collapse
alispat profile image
Alisson Patrick

Hi guys! I believe I found a simpler solution for this issue on ActiveStorage.

Just put this hidden_field on your form:

<%= f.hidden_field :image, id: nil, value: params[:post][:image] rescue nil %>
Enter fullscreen mode Exit fullscreen mode

And there is no need to do any treatment on your controller.

Your form will end up like this:

<%= form_with(model: @post, local: trye) do |f| %>
  <%= f.text_field :title %>
  <%= f.text_area :body %>
  <%= f.hidden_field :image, id: nil, value: params[:post][:image] rescue nil %>
  <%= f.file_field :image, direct_upload: true %>
<% end %>
Enter fullscreen mode Exit fullscreen mode