我跟着这个tutorial并遇到了困难。
我无法上传文件,我只能选择一个文件而且什么都没发生。 我想这个插件不起作用或者我没有添加一些必要的东西,如果有人帮助我,我将不胜感激。
Hier是我的路线.rb:
Try::Application.routes.draw do
match '/home' => 'home#index'
resources :uploads
root :to => "home#index"
end
uploads_controller.rb:
class UploadsController < ApplicationController
def home
end
def index
end
def create
@upload = Upload.new(params[:upload])
if @upload.save
render :json => { :pic_path => @upload.picture.url.to_s , :name => @upload.picture.instance.attributes["picture_file_name"] }, :content_type => 'text/html'
else
render :json => { :result => 'error'}, :content_type => 'text/html'
end
end
end
和views / home / index.html.erb:
<%= stylesheet_link_tag('jquery.fileupload-ui') %>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<%= javascript_include_tag 'jquery.fileupload', 'jquery.fileupload-ui' %>
<script type="text/javascript" charset="utf-8">
$(function () {
$('.upload').fileUploadUI({
uploadTable: $('.upload_files'),
downloadTable: $('.download_files'),
buildUploadRow: function (files, index) {
var file = files[index];
return $('<tr><td>' + file.name + '<\/td>' +
'<td class="file_upload_progress"><div><\/div><\/td>' +
'<td class="file_upload_cancel">' +
'<button class="ui-state-default ui-corner-all" title="Cancel">' +
'<span class="ui-icon ui-icon-cancel">Cancel<\/span>' +
'<\/button><\/td><\/tr>');
},
buildDownloadRow: function (file) {
return $('<tr><td><img alt="Photo" width="40" height="40" src="' + file.pic_path + '">' + file.name + '<\/td><\/tr>');
},
});
});
</script>
<div class="files">
<%= form_for @upload, :html => { :class => "upload", :multipart => true } do |f| %>
<%= f.file_field :picture %>
<div>Upload files</div>
<% end %>
<table class="upload_files"></table>
<table class="download_files"></table>
</div>
home_controller.rb:
class HomeController < ApplicationController
def index
@upload = Upload.new
end
end