Skip to content

Instantly share code, notes, and snippets.

@nsmith
Created September 16, 2010 16:51
Show Gist options
  • Select an option

  • Save nsmith/582742 to your computer and use it in GitHub Desktop.

Select an option

Save nsmith/582742 to your computer and use it in GitHub Desktop.
def create_directly
@attachment = Attachment.new
file_name = params[:file_name]
if request.body.class == StringIO
FileUtils.mkdir "#{Rails.root}/tmp/" + @attachment.id.to_s
file = File.open("#{Rails.root}/tmp/" + @attachment.id.to_s + "/" + file_name, "w")
file << request.body.string
file.close
@attachment.attachment = File.open("#{Rails.root}/tmp/" + @attachment.id.to_s + "/" + file_name, "r")
elsif request.body.class == Tempfile
@attachment.attachment = request.body
end
if @attachment.save
result = {}
result[:success] = true
result[:attachment_id] = @attachment.id.to_s
result[:thumb] = @attachment.attachment.url(:thumb)
result[:url] = @attachment.attachment.url
render :json => result, :status => 200
else
render :json => { "error" => "Your upload got messed up. Sorry." }, :status => 500
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment