我想加载config目录,即用户选择的yaml文件。 我有一个观点:
<h1>File Upload</h1>
<p>Upload your Yaml Configuration File</p>
<p style="color: green"><%= flash[:notice] %></p>
<%= form_for :uploadFile, :html => { :multipart => true } do |f| %>
<label for="upload_file">Select File:</label>
<%= f.file_field :upload %>
<%= f.submit "Upload", :disable_with => 'Uploading...' %>
<% end %>
我有一个控制器:
class UploadController < ApplicationController
def uploadFile
uploaded_io = params[:uploadFile][:upload]
File.open(Rails.root.join('public', uploaded_io.original_filename), 'w') do |file|
file.write(uploaded_io.read)
end
flash.now[:notice]="File has been uploaded successfully"
end
end
当用户选择文件并按“上传”时,没有任何反应,文件不在配置文件夹中。我能做什么?也许我应该使用“import yaml”并以某种方式加载它,怎么样?
日志说:
Started POST "/upload/uploadfile" for 127.0.0.1 at 2012-11-08 10:51:46 +0100
Processing by UploadController#uploadfile as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"5FKgZJG4gKPCHXe8OqhFVZL0bu2X9zLR3sxd4ELBulA=", "uploadFile"=>{"upload"=>#<ActionDispatch::Http::UploadedFile:0x3b03ad8 @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/RackMultipart20121108-2664-tb7lxr>>}, "commit"=>"Upload"}
[1m[35mSetting Load (0.0ms)[0m SELECT "settings".* FROM "settings" WHERE "settings"."id" = ? LIMIT 1 [["id", 1]]
Rendered upload/uploadfile.html.erb within layouts/upload (0.0ms)
Completed 200 OK in 0ms (Views: 0.0ms | ActiveRecord: 0.0ms)
答案 0 :(得分:1)
File.open(Rails.root.join('config', uploaded_io.original_filename), 'wb+') do |file|
file.write(uploaded_io.read)
end
试试这个