Home>

Save the image to a table using ActiveStorage
I want to display an image in a browser
You can access the data on your computer and the data is saved in the ActiveStorage table,
I am in trouble because I cannot save it in the image of the item table.
If i could teach me, it'll helps a lot.

There is an error in app/views/items/_item.html.erb: 1.

Contents that could not be deciphered

・ Last message of the terminal
ActiveStorage :: Attachment Load (0.4ms) SELECTactive_storage_attachments. * FROMactive_storage_attachmentsWHEREactive_storage_attachments..record_id= 8 ANDactive_storage_attachments..record_type='Item' ANDactive_storage_attachments..name='image' LIMIT 1
↳ app/views/items/_item.html.erb: 1
Rendered items/_item.html.erb (Duration: 3.4ms | Allocations: 1361)
Rendered items/index.html.erb within layouts/application (Duration: 54.2ms | Allocations: 17299)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 72ms (Views: 53.7ms | ActiveRecord: 12.8ms | Allocations: 25897)

Error message
Corresponding source code
Ruby on rails
app/views/items/_item.html.erb
  
    <% # Referenced code<% = image_tag message.image, class:'message-image' if message.image.attached?%>%>
    <% # attached? Method Whether a file is attached to the record Model.filename.attached?%>
      <% # image_tag method Rails helper method to generate img element%>
      <% # image_tag method generates an img element that displays an image by simply calling the image file from the model and describing it as an argument without specifying a complicated Rails directory path%>
What I tried

);">
Change from above to ↓
);">

in app/models/item.rb
Implement the following
class Item validates: text, presence: true
belongs_to: user
has_one_attached: image (I thought I could save it to image here)

Attach image file to record in item table No need to add columns to the item table

I thought it was different, but in items/show.html.erb
Implemented the same as _item.html.erb so that images can be saved

Supplementary information (FW/tool version, etc.)

Introduced to Gem

gem'mini_magick'
gem'image_processing','~>1.2'

What is mini_magick? It makes it possible to handle the functions of ImageMagick in Ruby.
Provides a function to adjust the image size, which cannot be provided by ImageProcessing and MiniMagick
ImageMagick required for image processing

  • Answer # 1

    Now I want to display the image saved in the active storage as the background image.

    <% = item.image%>I think it would be nice if I could get the URL of the image for the part of<% = url_for (item.image)%>Or# {url_for (item.image)}How about

  • Answer # 2

    Ruby on rails
    app/views/item/_item.html.erb
    In order to display in the background, the image cannot be displayed with item_tab,
    First, I edited the following file.
    config/environment/development.rb
    Add the following to the first line
    config.action_mailer.default_url_options = {host:'localhost'}
    By doing this, you can get the image as a url in the local environment,
    Images can now be displayed with the view's url_for method.
    (Https://qiita.com/shou8/items/3c41cb6a06ff1e0cdb95)