Carrierwave adds photos via the “hero-rails console” from s3 on the production side

I have a simple application that uploads an image that I save using wavewave in a blog database. (with title, body and image) and my credentials are working fine.

I have an image uploaded to an s3 account with this URL: / s 3.amazonaws.com/Buket_name/ .. path .. / thumb _smile.png

How to update a database using an image using the geku rails console. This does not seem to work:

b = Blog.new b.title = "a blog" b.body = "some text" b.image = File.new("s3.amazonaws.com/Buket_name/..path../thumb_smile.png","a") or b.image = File.open("s3.amazonaws.com/Buket_name/..path../thumb_smile.png","r") Errno::ENOENT: No such file or directory - s3.amazonaws.com/Buket_name/..path../thumb_smile.png 
+4
source share
1 answer

Use the CarrierWave attribute remote_ remote_{name}_url= for the simplest solution.

 b = Blog.new b.title = "a blog" b.body = "some text" b.remote_image_url = 'http://s3.amazonaws.com/Buket_name/..path../thumb_smile.png' b.save 

This function is specific to CarrierWave, so if you want to do something similar with another library, use open-uri from the standard library.

 require 'open-uri' image = open('http://s3.amazonaws.com/Buket_name/..path../thumb_smile.png') 

Now the image is a Tempfile that can be used as a file in your Ruby script.

 require 'open-uri' image = open('http://s3.amazonaws.com/Buket_name/..path../thumb_smile.png') b = Blog.new b.title = "a blog" b.body = "some text" b.image = image b.save 
+6
source

Source: https://habr.com/ru/post/1501378/


All Articles