I need to upload files in rails 3, I followed a lot of tutorials but
dont works for me, I did:
http://www.dzone.com/links/r/file_uploading_using_rails.html,
but I just can see the page with the option of upload a file…but
can’t save the file…or upload it, of course there something I’m not
doing…please healp, any way…here is my code, thanks in advance.
CONTROLLER
class UploadController < ApplicationController
protect_from_forgery :only => [:create, :update, :destroy]
def index
render :file => ‘app/views/upload/uploadfile.html.erb’
end
def upload
@post = [];
params[:images].each { | id, image |
result = UploadedFile.save(id, image);
@post << result if result
}
respond_to do |format|
format.html
end
end
end
MODEL
class UploadFile < ActiveRecord::Base
app/models/uploaded_file.rb
require ‘image_size.rb’
language UploadedFile < ActiveRecord::Base
def self.save(id, upload)
skipping empty fields
return false unless upload.blank?
The saved file name is composed of the unique id and original
extension
name = id + “.” + upload.original_filename.split(".").last
target directory to save files
directory = “subirarchivo/public/data”
create the file path
path = File.join(directory, name)
write the file
File.open(path, “wb”) { |f| f.write(upload.read) }
img = ImageSize.new(open(path))
return {
“width” => img.get_width,
“height” => img.get_height,
“filename” => name
}
end
end
VIEW
<!- app/views/upload/uploadfile.html.erb -->
Subir Archivos usando Rails y jQuery
Files
Images
<% form_for :upload, :html => { :multipart => true, :id =>
“upload_form” },:method => ‘post’,
:url => { :action => “upload” } do |f| %>
<% end %>**