What is a Ruby on Rails way to save images?

I develop on iOS, and I’m switching from a PHP backend to Ruby on Rails. The interchange format is JSON.

A quick Google search for ‘save images in Rails’ has nearly every result talking about saving image data as blobs to the database. I might be mistaken, but I’m under the impression that saving image data in a database is a huge waste of time and space (as opposed to saving a link to the file location (‘/img/subcat/4656.png’).

In PHP, it’s pretty standard to receive the data, generate a filename, save that file to disk, and update the database with the image’s location on disk. Is this the same for Ruby on Rails, or is there some built-in ActiveRecord image functionality I’m not aware of?

3

What I’ve done in the past is use PaperClip with Amazon S3 and CloudFront CDN for faster delivery. PaperClip supports S3 storage very nicely out of the box: see their documentation for S3 storage configuration options.

  1. Set up a CloudFront distribution to forward image requests to your S3 bucket and other requests for static assets to your Ruby on Rails application (alternatively, you can upload those to S3 as well, but that’s outside the scope of this question).
  2. CNAME a domain you control (such as assets.example.com) to point to your CloudFront distribution.
  3. In the PaperClip configuration, set s3_host_alias: assets.example.com. This will cause generated URLs to saved image resources to use your CloudFront distribution host.
  4. In your Ruby on Rails configuration, you can then do config.action_controller.asset_host = http://assets.example.com. Ruby on Rails asset URLs will then also use your CloudFront distribution, for caching/faster delivery.

That’s pretty sketchy, but it hopefully points you in the direction of some useful resources. Of course, that’s just one solution, but it’s one that has worked for me in a situation where I was dealing with hundreds of thousands of image attachments in the database.

(As a possible advanced step that may not be necessary or useful for your case, you can also set up multiple CNAMEs for your CloudFront distribution, such as assets0.example.com and assets1.example.com. Both paperclip s3_host_alias and action_controller.asset_host can then take lambdas that will allow you to deterministically (based on the asset) generate URLs that use different hosts, for better parallelization if you’re downloading a bunch of them on the same page, as browsers will limit the maximum simultaneous connections to the same host.)

I’ve tried several gems, but now I’m using FineUploader together with PaperClip

FineUploader handles a lot of the GUI for you, for example upload progress, drag-and-drop and multiple file uploads, and so on. I’m using PaperClip because I want to store the images on my servers instead of on S3.

What we normally do is look for a stable out-of-the-box configurable solution instead of recreating stuff from scratch. With PaperClip I store the images and with a StateMachine I do conversions on the images and put them into a category based on filename. I had the same question you had, and this was my best working solution, that’s why I share it.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật