How to get url of Active Storage image
For my User
which has_one_attached :avatar
I can get the url in my views with <%= image_tag url_for(user.avatar) %>
.
So, in controller I would use just url_for(user.avatar)
For Category
which has_one_attached :image
:
url_for(category.image)
How to retrieve attachment url with Rails Active Storage with S3
Use ActiveStorage::Blob#service_url
. For example, assuming a Post
model with a single attached header_image
:
@post.header_image.service_url
Update: Rails 6.1
Since Rails 6.1 ActiveStorage::Blob#service_url
is deprecated in favor of ActiveStorage::Blob#url
.
So, now
@post.header_image.url
is the way to go.
Sources:
- Link to the corresponding PR.
- Link to source.
Is it possible to display images from rails active storage inside another application using a URL?
I believe you should return the url ready to be used from your backend API instead of building the URL on your electron app.
You can find the correct url from your model using:
url_for(user.avatar)
There are some options to return it. If you don't mind making your model a bit dirty, you can create a method called avatar_url
and then return this value with your json.
# new method on model
def avatar_url
Rails.application.routes.url_helpers.url_for(avatar)
end
# on controller controller
def show
user = User.find(params[:id])
render json: user, methods: [:avatar_url]
end
A cleaner option is to create a jbuilder on to build the json for you, and add the url there:
# create new jbuilder file app/view/users/show.json.jbuilder
json.id @user.id
json.avatar_url url_for(@user.avatar) if post.avatar.attached?
# on controller controller
def show
@user = User.find(params[:id])
respond_to do |format|
format.json
end
end
How to return active storage url that points to an attachment (picture) in Rails app?
For an image on S3 I use this for my apps. You will need to change the @
variable to match what you are using and then the field you are using, I took a best guess based on your question. It would be easier going forward to always post the code you are using so we can provide better feedback
<%= @user.profile_picture.service_url %>
You'll notice that this doesn't work locally so if using both during dev is important to use something like this.
<%= Rails.env == 'production' ? @user.profile_picture.service_url : url_for(@user.profile_picture) if @user.profile_picture.attached? %>
that will give you the path to the image, then use it however you are, for example, if it is a background image
<div style='background-image:url("<%= Rails.env == 'production' ? @user.profile_picture.service_url : url_for(@user.profile_picture) if @user.profile_picture.attached? %>");'>
Active Storage Permanent Image URL
I think what you're after isn't easily possible. Active Storage doesn't seem to support permanent, non-expiring URLs: "Request has expired" when using S3 with Active Storage
Depending on your setup, there might be a useful and (mostly) hack-free workaround. In my case, I've set a custom show
action on the record that owns the file I want to link to:
redirect_to url_for(@record_name.file)
Then, using a path helper for the record show
action in my app, as usual, just renders the thing I want via the expiring url_for
.
Rails 5.2 API ActiveStorage how to get URL paths for multi image?
Here is the solution:
def images
return unless object.images.attachments
image_urls = object.images.map do |image|
URI.join(
ActionController::Base.asset_host,
rails_blob_path(image))
end
image_urls
end
I have called an image from Active Storage with url_for. Is there any way, to link that image with the post that belong to? in rails
Wrap your image in link tag passing link to show
action. I assume there is a routes configure and available as helper post_path
<%= link_to post_path(post) do %>
<%= image_tag post.image, class="index_images" if post.image.attached? %>
<% end %>
Related Topics
How to Check the Gem Version in Ruby at Runtime
How Much Performance Do You Get Out a Heroku Dynos/Workers
Ruby's <=> Operator and Sort Method
Error Installing Gem Capybara-Webkit
Raise Custom Exception with Arguments
Break and Return in Ruby, How to Use Them
How to Dump an Http Request from Within Sinatra
Return Two and More Values from a Method
Finding Nil Has_One Associations in Where Query
Purpose of & (Ampersand) in Ruby for Procs and Calling Methods
How to Save Settings as a Hash in a External File
Why Does Ruby "Script/Generate" Return "No Such File or Directory"
Ruby Koans: Explicit Scoping on a Class Definition Part 2
Thread.Join Blocks the Main Thread
How to Resolve "Gpg: Command Not Found" Error During Rvm Installation