简体   繁体   中英

Rails: Image Upload with Tableless Model

Here is the scenario, i would like the user to input all the data and all and use em to populate a result. I won't need to store them in a database since i will just be showing them a result page.

I did http://railscasts.com/episodes/219-active-model and made my model tableless.

But now i have a problem when i wanna receive image upload from the user. I would also like to display that picture in the result page, and since i will just be using it once, if possible i wouldnt wanna store it in the database as well.

I tried implementing paperclip with the tableless model (since i couldnt find any other solution) but it seems that the model has inherit ActiveRecord::Base for it to work...

Is this possible? Or is this other way i can implement this?

Thanks!

If you were to succeed with using Paperclip for this, how would you get rid of the uploaded image once you no longer needed it? Without a database or some other form of persistent storage, how would you know where the image had been stored?

I think that you have some conceptual issues here that you should rethink before you start hacking up tableless models that accept image uploads.

But, if for some reason you really want to do it this way, then I would suggest just uploading the image without the benefit of a gem like Paperclip, which is really intended to make it easier to associate files with ActiveRecord objects. Just google for how you upload a file in Ruby, it's not really all that difficult.

OK, so you want to receive an image, and then display it right back, and not store the image. Can do.

What about a Controller that receives a file using multipart, and then transmits the file back to the request?

Controller file:

def upload
  # Save file
  name =  params['datafile'].original_filename
  directory = "tmp/uploads"
  temp_file_name = File.join(directory, name)
  send_file temp_file_name, :status=>200
end

You'll then just cleanup tmp when you need. Or, try out doing a File.delete temp_file_name when you need to.

If you want to validate that it's an image, you can do Paperclip model validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM