Store Image in Database Using Rails Paperclip Plugin

Store image in database using rails paperclip plugin

Check out this sample app

http://patshaughnessy.net/2009/5/29/paperclip-sample-app-part-3-saving-file-attachments-in-a-database-blob-column

I guess this is exactly what you need.

HTH

PS: storing images in db is usually a bad idea, I am sure you can use paperclip with S3 / cloudfront ( as mentioned in the answers below )

Image file uploader plugin for Rails 3.2 that can store to Database

Here is an example how to you can store an image in binary field (raw_file)

form:

<%= form_for @user do |f| %>
<%= f.file_field :avatar %>
<% end %>

controller:

def create
@user = User.new(params[:user])
# store uploaded avatar as blob
@user.raw_file = params[:user][:avatar].read
@user.save
end

Use RMagick or mini_magick to convert image from blob.

Upload image with Paperclip Problem

If you want to create tableless model - there are already set of solutions, such as link text

How to seed Rails db with images from the asset pipeline (paperclip)?

Public Images

If you are referencing an image in the public folder, store the root-relative path to the image as a string in the database. (E.g., if your image file is project/public/images/pic.jpg, then you would have image: '/images/pic.jpg' in the seeds.

Asset Managed Images

If you are referencing an image in your assets, things can get a touch more complicated (unless you’ve ill-advisedly turned off the config.assets.digest config). You’ll want to use the path to the asset in the seeds file (like with the first part of this answer referencing files in the public folder), but then when you want to link to the image in your views you’ll have to use Rails’ image_tag to get the asset-managed url for the image.

If for some reason you want to be able to get the asset-managed path to the image in any way other than image_tag in views, it gets a bit complicated. You can find some more details on working with the asset pipeline in the official Rails guide to The Asset Pipeline.

Images stored in Database

If you are storing the image file data in the database, you would use a binary field (not string). You could load the image data with a call to File.read. E.g.:

image_data = File.read('path/to/image.jpg')
Model.create!([
{
...
image: image_data
},
...
])

You’ll need to add a custom controller method & route to deliver the image data as a file to the client (be sure to handle the mime type correctly for that).

Personally, in those cases where I’ve chosen to store raw file data in the database, I prefer to have a separate table just for the data and then join it to the main model as needed. (In this case, there would be your existing images table, and it would be joined to a table named something like image_files with just one field, aside from id):

create_table :image_files do |t|
t.binary :data
end

And then you could reference that from your images table:

t.belongs_to :image_file

(or put the reference to the images table in the image_files table.)

As to the arguments for/against storing raw file data in a database, in general it’s sub-optimal (you lose the benefits of having the web-server directly sending the files from the filesystem and have to have Rails intervene). But it can make sense in some cases (such as when you want to restrict access — e.g., to specific users).

Seeding a model with a paperclip image in ruby on rails

You can just create ActiveRecord Model as below.

# create Event record with paperclip file
Event.create(name: 'event1', photo: File.new("image/to/path.jpg"))

This is Event model:

class Event < ActiveRecord::Base
has_attached_file :photo
end

Ruby on Rails: Paperclip with multi-step-form

If you really don't want the image hitting your database at all before the user can check it, you could use JavaScript to display the image immediately. Otherwise you'd need to save it somewhere on the backend, whether through Paperclip or otherwise.

If you only want to use Rails and no JavaScript, I'd just save the image, display it back to them, and give them the option to delete it.

How to store an array of images in Rails 3?

You should probably create a table and model for each Screenshot and then set up a has_many relationship from Book to Screenshot, and a belongs_to from Screenshot to Book.

Then Screenshot would use the paperclip plugin to handle the attachment in whatever way you like, though I recommend storing it either on the filesystem or on something like Amazon's S3, both of which the plugin should help you do. Whatever you do, don't store them in the database as it's horribly inefficient and doesn't let you take advantage of fast file servers like Apache, Nginx, or S3.

How to store images in google storage using rails?

For Paperclip, here is how you store it on Amazon's S3. But you can change storage to whatever you want by editing the path and url options in has_attached_file. See here



Related Topics



Leave a reply



Submit