How to add an image path to the image column with railsc to heroku

Asked 2 years ago, Updated 2 years ago, 109 views

I'd like to add a picture with heroku run railsc to the project that works with heroku.

Currently, we have added data by specifying the path of the image.

pry(main)>Article.create(title: 'Development fun', content: 'Especially Rails' image: File.open("app/assets/images/dev/Rails.jpg", "r"))

However, if you look at it on the web, it says %e9&87% 91%... instead of an image.
What does this stand for?

Also, when I looked into it, it said that I would put the static file to be published in public/assets, but if I specified the path in File.open, it would play No such file or directory@rb_sysopen.

I would like to know how to upload images from heroku railsc.
Thank you for your cooperation.

ruby-on-rails heroku

2022-09-30 18:28

2 Answers

If you want to keep the uploaded file data in the database as it is, using File.open will not work.The reason is that as far as the language reference, the return value of File.open appears to be the File object itself, not the data of the file.

(Excerpt from File.open description)

Open the file specified in path and generate and return a File object.

If you want to read files from the file system, you can use the read method.

(Excerpt from IO#read)

read(length=nil, outbuf="")->String

Read length bytes and return the string.
binary read method if the length argument is specified,
Otherwise, it acts as a text read method. Returns nil if EOF is already reached. However, length
Returns an empty string " を if is specified as nil or 0.
For example, open {|f|f.read} is \"

.

I couldn't check it in my environment, so I answered by looking at the language reference.I'm sorry, but please check the actual execution.


2022-09-30 18:28

Let's save Heroku to S3 because the file disappears when dyno is restarted.I can't say anything about the image because it depends on the implementation.

https://devcenter.heroku.com/articles/s3


2022-09-30 18:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.