上传文件ajax调用

时间:2017-04-11 13:42:33

标签: ruby-on-rails ajax

我正在尝试通过ajax调用上传和呈现csv,但我收到此错误:" ActionController :: UnknownFormat"。我做错了什么?

控制器:

class HomesController < ApplicationController
  def index
    @items = []
  end

  def upload
    myfile = params[:file]
    @items = CSV.read(myfile.path)
    respond_to do |format|
      format.js
    end
  end
end

查看:

<%= form_tag(homes_upload_path, multipart: true, remote: true, :authenticity_token => true) do %>
  <%= file_field_tag :file %>
  <%= submit_tag( "Import" ) %>
<% end %>

<div>
  <div id="items_grid" >
    <%= render partial: 'items_list', locals: {items: @items}  %>
  </div>
</div>

upload.js.erb:

$("#items_grid").html("<%= escape_javascript(render partial: 'items_list', locals: { items: @items } ) %>");

1 个答案:

答案 0 :(得分:1)

Rails remote true不支持附件。请使用'remotipart'gem来实现此功能。

gem 'remotipart', '~> 1.2'

bundle install

//= require jquery.remotipart

Remotipart Gem