seed images using carrierwave - ruby-on-rails

I am trying to create a Angular 4 app that uses a rails API to display and upload images. to upload images using the rails API I have decided to use carrierwave. what I would like to know is how do I seed some initial images and store the url in the database? below is what I have done in my db.seed file
Pictures.create!(
image: Rails.root.join("C:/Ciaran/admin/src/assets/images/test_img1.jpg").open,
)
However where is the image stored in my rails app folder? I do not see it anywhere.
And when I inspect the pictures table in MySQL for the image column the whole url is not displayed only test_img1.jpg? How do I make it show the full url?
So basically if someone who knows more about rails and carrierwave could please explain to me:
For the code snippet that I have shown above does that create a copy of the image into my rails app? because I do not see it in any of the apps folders once I have seeded the database?
why is the full url not displayed in the database table for pictures and how do I get it to display the full url so I can access it from my angular front end?
Thank you for your help

1 - in your app folder there should be a file called image_uploader.rb. And it should have a def store_dir which indicates where is your files are stored.
2 - because full url is generated based on the above image_uploader.rb configuration file. You can simply get the url for example if your model instance variable is #picture
#picture = Picture.first
#picture.image.thumb.url

Related

Vue Js & Rails: How to display image attached with blob

I'm uploading image files using Rails Active Storage gem and they are being hosted in AWS S3. However, I wonder how could I display the image attached to each record in my Vue Js component. I have tried to directly call the image file name but it did not work; Also, I tried to use the AWS image_url but after some time I realized that it was an incorrect approach. What would be your suggestions to accomplish this?

Copy images from one S3 bucket to another with specified path prefixes

I have an S3 bucket setup already, it contains images of a photo gallery, uploaded from my Rails app using PaperClip. Images are accessible via some arbitrary URLs like: http://s3.amazonaws.com/oldbucket/images/files/000/001/920/original/40a6885fc09c8ed4e1e3745d7f7fb770.jpg?1415766995.
Kindly advice me the best option considering following requirements:
I have to copy those images to another S3 bucket in another AWS
account
I want to make the new image URLs according to specific
patterns, like: .../newbucket/{userid}/{galleryid}/{image-size}.jpg
I want to create multiple versions of each image, according to size
(original, thumbnail and icon)
Any options using Rails gem or software that would do above would be helpful.
Thanks
For this you need to add carrierwave gem for saving image from remote url. You can also do with paperclip.
First create a seed file without adding carrierwave uploader to your app. I am considering User as model and avatar as image.
User.all.each{|u| puts user.avatar.url}`
Now remove paperclip and add carrierwave This will give you list of all images. Now add it to seed file for model you want to add this images.
class Modelx
mount_uploader :avatar, AvatarUploader
end
So your seed file should have entries like.
Modelx.create([{:avatar_remote_url => image_url1}, {:avatar_remote_url => image_url2},.....])
You can set specific path and also create multiple dimension images using carrierwave.
REfrence url for carrierwave here.

Upload file in Rails 3.2 app - develop engine or use gem?

We are looking to add a simple file uploader to our rails 3.2 app which is a business application (with Rails engines). Here are what we are looking for with the file uploader:
Allow access control to who can do what. For example, sales can upload a contract and acct can view the uploaded contract.
No change to current model. The file uploader acts on its own about file uploading, checking, storing and removing. We are thinking to have a file uploader engine and attach the engine to the Rails app.
File uploaded belongs to a model. For example, uploaded contract copy belongs to a project.
May need to upload file to a remote server.
We are evaluating options of developing our own uploader engine or find a upload gem such as carrierwave or paperclip. Can someone shed a light on rails file uploading and its related issue?
Using a combination of cancan and paperclip is a good option.

file paths will not update after file upload in rails production environment

I have a rails application which will upload image with the name as same as the book id to assets/books_icon when the user creates a book.
upload_icon(params[:book][:image_upload])
# upload_image when create/update book
def upload_icon(uploaded_io)
photo_directory = "app/assets/images/"
# only when user upload the iphoto
if uploaded_io.present?
# upload to the exact location with category_id
extension = File.extname(uploaded_io.original_filename)
photo_location = 'books_image/'+ #book.id.to_s + extension
# open in binary mode
photo_full_location = photo_directory + photo_location
File.open(photo_full_location, 'wb') do |file|
file.write(uploaded_io.read)
end
# only have to state which is the directory,
# image_tag will use assets piepline which will add 'assets/images/' as prefix in src
#book.update_attribute(:image_url, photo_location)
end
end
It works in the development mode.Then, I deployed the rails application with passenger on mac Apache2 server with mysql as the database and I changed config.assets.compile = true in the development configuration file.
In the production mode, I can create the new book and I can upload the image to assets/books_icon. However, the file path for the image which I just upload will not update.
For example, I create a new book with id 2, and there is 2.jpg in assets/books_icon. But the rails application will told me http://localhost/assets/images/books_icon/2.jpg is missing.
However, when I restart the apache server, I can view the picture at http://localhost/assets/images/books_icon/2.jpg
Is there any solutions to solve this type of caching problem??
In this context, an uploaded image isn't really considered an "asset" -- think of it as data that you happen to be storing somewhere else. (An image asset might be a logo, or background image, etc.). Rails' asset pipeline does some pretty tricky stuff in order to resolve a simple file path to an actual resource (e.g. image) when it's an asset.
By default, Rails makes the app/public folder the document root, and thus a place that you could upload images to -- perhaps in a subdirectory called "upload/img" in which case you could reference it with the path /upload/img/mybook.jpg.
This approach tends to be fragile, however because the image is really directly associated with data in your database, but located on the filesystem of a server. It starts falling apart when you move from development to stagings or production servers.
One approach I would not recommend is to upload the image and store it as a blob type in your database. Another I would recommend is to have another "central" server that you can upload images to that acts as an extension to your database. Many people use Amazon AWS "S3" service for this kind of thing. Take a look at the CarrierWave gem which does an excellent job of making all of this really easy, flexible, and powerful.

How should I parse a file in Ruby on Rails to store in a database?

I want to create a simple form for users to upload a file, which will be stored in a database. I also want to display all the submitted files in the database with their name and a link to download the file. Schematically, what's the best way to do this in Rails/how should I store the file in table (which fields should my table have? etc). Thanks!
i would use paperclip gem with the upload to s3 instead of file system
https://github.com/thoughtbot/paperclip
checkout the README, most of the examples are for an image, but works with non-image files as well
use paperclip to upload file, you can store images/file in your database as well as in s3 (AWS)
See below link how to use paperclip in rails with example
Here is the steps how to upload file using paperclip in rails
http://patshaughnessy.net/2009/4/30/paperclip-sample-app
for github
https://github.com/thoughtbot/paperclip
https://github.com/websymphony/Rails3-Paperclip-Uploadify

Resources