我很疯狂将文件上传到我的ruby 3.2.8应用程序中。我需要你的帮助!我试过这段代码:
Why yaml file upload does not work? I think permission problems
但它不起作用,不在“public”中创建文件,而只创建临时文件。今天我发现了一种新的方式:
Rails 3 - upload files to public directory
所以,查看:
<p style="color: green"><%= flash[:notice] %></p>
<%= form_for :uploadFile, :url => "/upload/uploadfile", :html => { :multipart => true } do |f| %>
<label for="uploadFile">Select File:</label>
<%= f.file_field :upload %>
<%= f.submit "Upload", :disable_with => 'Uploading...' %>
<% end %>
控制器:
class UploadController < ApplicationController
def uploadFile
require 'fileutils'
tmp = params[:uploadFile][:upload].tempfile
file = File.join("public", params[:uploadFile][:upload].original_filename)
FileUtils.cp tmp.path, file
flash.now[:notice]="File has been uploaded successfully"
end
end
但同样的问题。在“公共”中没有文件:(
日志说:
Started POST "/upload/uploadfile" for 127.0.0.1 at 2012-11-12 11:19:04 +0100
Processing by UploadController#uploadfile as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"WI23X2HeanJpQJoRFc8NUJ078yLYpKgRE3VnJDBeccA=", "uploadFile"=>{"upload"=>#<ActionDispatch::Http::UploadedFile:0x47e4238 @original_filename="newCases.yml", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"uploadFile[upload]\"; filename=\"newCases.yml\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:C:/Users/Simo/AppData/Local/Temp/RackMultipart20121112-4716-1geiytj>>}, "commit"=>"Upload"}
[1m[36mSetting Load (0.0ms)[0m [1mSELECT "settings".* FROM "settings" WHERE "settings"."id" = ? LIMIT 1[0m [["id", 1]]
Rendered upload/uploadfile.html.erb within layouts/upload (0.0ms)
Completed 200 OK in 16ms (Views: 0.0ms | ActiveRecord: 0.0ms)
啊,上传文件是yaml,改变什么?
我的宝石文件:
source 'https://rubygems.org'
gem 'rails', '3.2.8'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
gem 'sqlite3'
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem "comma", "~> 3.0.3"
gem 'fastercsv', '1.5.4'
答案 0 :(得分:1)
可能添加:url to form_for like
form_for :uploadFile, :url => "/upload/uploadfile"
会有帮助吗? 使用rails 3.2.8,您的上传工作正常,但我添加了:url参数。