[Ruby on Rails] I want to get the URL of the image saved in Active Storage


If I thought that I could easily get the URL of the image saved in ActiveStorage, I unexpectedly used it, so make a note.

image_Erb using tag

<%= image_tag recipe.image.variant(resize: '200x200').processed %>

Actual html

<img src=\"http://localhost:3000/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d3fb1fe3b017a6060dd908f2463551e10cfbacb0/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9MY21WemFYcGxTU0lNTWpBd2VESXdNQVk2QmtWVSIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--306915654d885f72af9e428f63f831384a058376/01.png\" />

I just want the URL without using image_tag.


I got it by using rails_representation_url.

Validate on console

irb(main):001:0> Rails.application.routes.url_helpers.rails_representation_url(recipe.image.variant(resize: "200x200").processed, only_path: true)
=> "/rails/active_storage/representations/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBCZz09IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--d3fb1fe3b017a6060dd908f2463551e10cfbacb0/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaDdCam9MY21WemFYcGxTU0lNTWpBd2VESXdNQVk2QmtWVSIsImV4cCI6bnVsbCwicHVyIjoidmFyaWF0aW9uIn19--306915654d885f72af9e428f63f831384a058376/01.png "

It may be information that has been released, but just in case.

Reference information

How can I get url of image variant in model (outside of controller/view)? Active Storage https://stackoverflow.com/questions/53542627/how-can-i-get-url-of-image-variant-in-model-outside-of-controller-view-active

Recommended Posts

[Ruby on Rails] I want to get the URL of the image saved in Active Storage
I want to get the value in Ruby
How to implement image posting function using Active Storage in Ruby on Rails
I want to change the value of Attribute in Selenium of Ruby
Get the URL of the HTTP redirect destination in Ruby
[Rails + Webpacker] I want to use images of assets! Until you can view the image in Vue.js
[Active Admin] I want to specify the scope of the collection to be displayed in select_box
[Rails] I want to display the link destination of link_to in a separate tab
I want to transition to the same screen in the saved state
[Ruby] I want to reverse the order of the hash table
[Rails] How to get the URL of the transition source and redirect
I want to add a browsing function with ruby on rails
I summarized the flow until implementing simple_calendar in Ruby on Rails.
Things to remember and concepts in the Ruby on Rails tutorial
[Android] I want to get the listener from the button in ListView
part of the syntax of ruby ​​on rails
[Ruby] I want to output only the odd-numbered characters in the character string
[Rails] I want to send data of different models in a form
[Ruby] I want to extract only the value of the hash and only the key
[Ruby] I want to display posted items in order of newest date
I want to get the IP address when connecting to Wi-Fi in Java
How to solve the local environment construction of Ruby on Rails (MAC)!
I tried to make full use of the CPU core in Ruby
How to debug the processing in the Ruby on Rails model only on the console
I want to get the field name of the [Java] field. (Old tale tone)
[Ruby On Rails] How to search the contents of params using include?
Get the value of enum saved in DB by Rails with attribute_before_type_cast
Change the save destination of the image to S3 in the Rails app. Part 2
[Ruby on Rails] Until the introduction of RSpec
I tried to organize the session in Rails
I want to use arrow notation in Ruby
Recommendation of Service class in Ruby on Rails
I want to var_dump the contents of the intent
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to make the link destination part of the specified id
I want to get the value of Cell transparently regardless of CellType (Apache POI)
I want to play a GIF image on the Andorid app (Java, Kotlin)
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
I want to recursively get the superclass and interface of a certain class
[Java] How to get the URL of the transition source
Get the URL of the HTTP redirect destination in Java
[Ruby on Rails] How to write enum in Japanese
I want to embed any TraceId in the log
[Ruby on Rails Tutorial] Error in the test in Chapter 3
Docker the development environment of Ruby on Rails project
[Ruby on Rails] How to change the column name
The story of migrating from Paperclip to Active Storage
[Ruby on Rails] Change URL id to column name
I want to use a little icon in Rails
Source used to get the redirect source URL in Java
I want to know the answer of the rock-paper-scissors app
I want to display the name of the poster of the comment
[Ruby On Rails] How to reset DB in Heroku
[Ruby on Rails] Post image preview function in refile
I want to define a function in Rails Console
[Rails] How to get the contents of strong parameters
I can't get out of the Rails dbconsole screen
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!