I am new to rails so sorry if this is easy. I am wondering the best way to upload pictures and display them in Ruby on Rails. I have a blog and would like to have the option of attaching a picture when creating a post.
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- How to specify memcache server to Rack::Session::M
- Why am I getting a “C compiler cannot create execu
相关文章
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
- “No explicit conversion of Symbol into String” for
- form_for wrong number of arguments in rails 4
CarrierWave is probably the best solution for picture uploading in Rails. The following post describes a solution for image uploading using CarrierWave while image transformations are done seamlessly in the cloud. Uploaded images are stored in the cloud and delivered through a CDN. No need to install RMagick, MiniMagick and ImageMagick. http://cloudinary.com/blog/ruby_on_rails_image_uploads_with_carrierwave_and_cloudinary
Many recommend to use
Carrierwave
and I am not an exception but I wanted to point one thing out. On many sites written with RoR (it's obviously not only RoR issue though) I notice that the request that's sending the picture can be pending for like 2-3 secs and often even more which ties up the application instance (which is probably not that terrible if you're using threads or don't care much about performance but it definitely is if you're using unicorn). So I'll explain how to avoid that using carrierwave:1) Pick some background worker for your app (I usually go with Sidekiq)
2) Add
carrierwave_backgrounder
to your Gemfile and configure it to work with your background worker and carrierwave (everything in its readme)I usually have control over my directories so I go with
store_in_background
option ofcarrierwave_backgrounder
which processes and stores the picture in the file system or S3 or whatever you're using3) Now, when you update the picture, its processing and storing goes to background , which frees your application instance but it takes a while, in most cases more than 1 second, and you need to return some response to the user
4) The easiest way to provide user with some response is to return javascript in which you change the old picture to a gif with a spinner or something like that and set javascript
SetInterval
function that checks if the picture has been processed (carrierwave_background
provides a function that can change the boolean column of your model to true when it's done processing) sendingAJAX
request every 2 seconds or whatever you prefer and change the picture when it's been processed.Now you have an untied application instance and a fine user experience at the same time.
PS I am new to rails and web development per se so this guide may have some caveats that I've missed
Hope it'll help somebody.
Oh, and by the way, there's this new gem called
refile
, it's amazing and can be a really good option in some cases.Many people recommend PaperClip. Perhaps you want to try using that first.
Assuming you don't need fancy features, don't wish to add a dependency and want to store the image as a BLOB in your DB, you can do something like:
Model:
Controller:
Here's a link to a more complete tutorial.
Sept 2018
For anyone checking this question recently, Rails 5.2+ now has ActiveStorage by default & I highly recommend checking it out.
Since it is part of the core Rails 5.2+ now, it is very well integrated & has excellent capabilities out of the box (still all other well-known gems like Carrierwave, Shrine, paperclip,... are great but this one offers very good features that we can consider for any new Rails project)
Paperclip team deprecated the gem in favor of the Rails ActiveStorage.
Here is the github page for the ActiveStorage & plenty of resources are available everywhere
attachment_fu (
http://github.com/technoweenie/attachment_fu
) is another option although I personally would recommend paperclip. It doesn't require Rmagick which is a big plus, and it supports some cool features like uploads to S3 with minor configuration.