Simone Mazzotta
Simone Mazzotta

Reputation: 71

Why yaml file upload does not work? I think permission problems

I would like to load in the config directory, a yaml file chosen by the user. I have a view:

<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 %>

and I have a controller:

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

When the user selects the file and press "upload" nothing happens and the file is not in the config folder. What can I do? Maybe I should use "import yaml" and load it in a certain way, how?

The log said this:

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)

Upvotes: 0

Views: 610

Answers (1)

yatish mehta
yatish mehta

Reputation: 915

File.open(Rails.root.join('config', uploaded_io.original_filename), 'wb+') do |file|
    file.write(uploaded_io.read)
end

Try this instead

Upvotes: 1

Related Questions