Uploading Pictures Ruby on Rails

2019-01-21 06:35发布

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.

8条回答
Lonely孤独者°
2楼-- · 2019-01-21 07:05

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

查看更多
戒情不戒烟
3楼-- · 2019-01-21 07:10

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 of carrierwave_backgrounder which processes and stores the picture in the file system or S3 or whatever you're using

3) 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) sending AJAX 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.

查看更多
该账号已被封号
4楼-- · 2019-01-21 07:10

Many people recommend PaperClip. Perhaps you want to try using that first.

查看更多
Bombasti
5楼-- · 2019-01-21 07:13

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:

class Image < ActiveRecord::Base
  def img=(input_data)
    self.filename = input_data.original_filename
    self.filetype = input_data.content_type.chomp
    self.img = input_data.read
  end
end

Controller:

class ImagesController < ApplicationController
  def display_img
    @img = Image.find(params[:id])
    send_data(@img.img, :type => @img.filetype, :filename => @img.filename,
              :disposition => 'inline')
  end
end

Here's a link to a more complete tutorial.

查看更多
成全新的幸福
6楼-- · 2019-01-21 07:23

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

查看更多
forever°为你锁心
7楼-- · 2019-01-21 07:24

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.

查看更多
登录 后发表回答