Simone Mazzotta
Simone Mazzotta

Reputation: 71

file upload is not successful ,why?

I'm going crazy to upload a file into my ruby 3.2.8 application. I need your help! I tried this code:

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

but it does not work, does not create the file in "public", but only creates the temporary file. Today I found a new way:

Rails 3 - upload files to public directory

So, View:

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

Controller:

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 

but same problem. in "public" there is no file :(

Logs says:

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)

ahh, the upload file is yaml, change anything?

My gemfile:

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'

Upvotes: 1

Views: 361

Answers (1)

Shamir K.
Shamir K.

Reputation: 395

Maybe adding :url to form_for like

form_for :uploadFile, :url => "/upload/uploadfile"

will help? With rails 3.2.8 your uploading works fine, except I added :url parameter.

Upvotes: 1

Related Questions