0

I have a form with a dropzone for image upload. I have followed the example: Medium: Rails Dropzone Ajax. The example works fine, but I would like to refresh a partial with AJAX after the upload is completed. I have modified the code to the following:

Controller:

class MediaContentsController < ApplicationController
  before_action :set_media, only: [:show]
  respond_to :html, :js

  def index
    @media_contents = Media.where(user_id: current_user.id)
  end

  def create
    @media = Media.new(file_name: params[:file], user_id: current_user.id)
    if @media.save!
      @media_contents = Media.where(user_id: current_user.id)
      MediaContentMailer.new_message(@media).deliver_later(wait_until: 1.minute.from_now)
      respond_to do |format|
        format.js
        format.html
      end
    else
      puts 'Hello, the processed failed'
      render json: { error: 'Failed to process' }, status: 422
    end
  end
end

View:

<div class="card-deck">
      <div class="col-md-6" style="padding-top: 30px;">
      <%= form_tag('/media_contents', method: :get, class: "dropzone stripe-2", id: "media-dropzone", remote: true) do %>
          <input name="authenticity_token" type="hidden" value="<%= form_authenticity_token %>" />
          <div class="fallback">
            <%= file_field_tag "media", multiple: true%>
          </div>
        <% end %>
      </div>
        <div class="col-md-6">
          <%= form_tag '/delete_media', method: :delete do %>
            <div id="media-contents" class="row">
              <%= render :partial => 'media_contents/show', :locals => {:media_contents => @media_contents} %>

              <div style="text-align: center; width: 100%;">
                <hr>
                <%= link_to 'Delete All', delete_all_path, method: :delete, id: 'delete-all', class: 'btn btn-success', disabled: @media_contents.empty? %>
              </div>
            </div>
          <% end %>
        </div>
    </div>

Create.js.erb

$("#media-contents").html("<%= j render partial: 'media_contents/show', :locals => {:media_contents => @media_contents} %>");

The goal is to refresh the media_contents/show with _show.html.erb after the upload so users do not have to refresh the page. But i get the following error:

ActionController::UnknownFormat (ActionController::UnknownFormat):

app/controllers/media_contents_controller.rb:15:in `create'

With line 15 being:

format.js

I have been playing with this for 2 days, any help would be welcome please. i am sure I am not far.

Rene Chan
  • 864
  • 1
  • 11
  • 25
  • see my answer here: https://stackoverflow.com/a/60336646/2144199 – larz Feb 21 '20 at 11:25
  • Does this answer your question? [ActionController::UnknownFormat (ActionController::UnknownFormat) in create action with format.js](https://stackoverflow.com/questions/51524206/actioncontrollerunknownformat-actioncontrollerunknownformat-in-create-acti) – larz Feb 23 '20 at 18:44

0 Answers0