:remote => true not working when a file uploader is used in form

后端 未结 3 663
轮回少年
轮回少年 2021-01-07 18:47

Basically when I include and use a file uploader in my form it seems to cancel out the :remote => true functionality and processes the action as HTML in place of JS. any ide

3条回答
  •  礼貌的吻别
    2021-01-07 19:27

    You can't upload files via AJAX, so apparently your request comes as plain HTML, because you don't have anything specific to :js and rails thinks it's just a plain HTML POST request.

    https://github.com/JangoSteve/remotipart

    gem 'remotipart', '~> 1.2'
    

    and then bundle install

    //= require jquery.remotipart
    

    sample_layout.html.erb

    <%= form_for @sample, :html => { :multipart => true }, :remote => true do |f| %>
      
    <%= f.label :file %> <%= f.file_field :file %>
    <%= f.submit %>
    <% end %>

    in Controller

    def create
      respond_to do |format|
        if @sample.save
          format.js
        end
      end
    end
    

    create.js.erb

    // Display a Javascript alert
    alert('success!');
    <% if remotipart_submitted? %>
      alert('submitted via remotipart')
    <% else %>
      alert('submitted via native jquery-ujs')
    <% end %>
    

提交回复
热议问题