Guest User

Untitled

a guest
Jun 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. class FileUpload < ActiveRecord::Base
  2.  
  3. ###############################################
  4. ################# VALIDATIONS #################
  5. validates_presence_of :upload_file
  6. validates_format_of :upload_file,
  7. :with => %r{\.(pdf|jpg|png)$}i,
  8. :message => 'Must be a URL for PDF, JPG or PNG file.'
  9.  
  10.  
  11. def self.save(upload,id)
  12.  
  13. name = upload.original_filename
  14. Dir::mkdir 'public/floorplans/' + id.to_s
  15.  
  16. directory = "public/floorplans/" + id.to_s
  17.  
  18. # create the file path
  19. path = File.join(directory, name)
  20.  
  21. # write the file
  22. File.open(path, "wb") { |f| f.write(upload.read) }
  23.  
  24. end
  25. end
  26.  
  27.  
  28.  
  29. -------------------------------------------------
  30. # Controller
  31. #Uploading the file
  32. post = FileUpload.save(params[:upload_file],@floorplan.id)
  33.  
  34. ---------------------------------------------------
  35. # view
  36. <% form_tag( {:controller => :floorplan, :action => :new, :id => @outlet.address }, :multipart => true) do %>
  37. <div>
  38. file: <input type="file" name="upload_file"> <br />
  39. <input value="Upload" type="submit">
  40. </div>
  41.  
  42. <% end %>
Add Comment
Please, Sign In to add comment