Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created February 6, 2015 19:29
Show Gist options
  • Save mayfer/127b091ed62cac503348 to your computer and use it in GitHub Desktop.
Save mayfer/127b091ed62cac503348 to your computer and use it in GitHub Desktop.
File upload
class FilesController < ApplicationController
def index
end
def upload
upload = params[:babyfile]
name = upload.original_filename
directory = "public/data"
# create the file path
path = File.join(directory, name)
# write the file
File.open(path, "wb") { |f| f.write(upload.read) }
render :text => "File has been uploaded successfully"
end
end
<form method="post" enctype="multipart/form-data" action="<%= upload_path %>">
<%= hidden_field_tag :authenticity_token, form_authenticity_token %>
<input type="file" name="babyfile" />
<input type="submit" name="Upload" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment