How can I get url of my attachment stored in active storage in my rails controller
Asked Answered
O

4

16

How can I get url of my has_one model attachment stored in active storage in my rails controller. So, that I would be able to send it as full link as api in json. So far, I have tried following methods but each of them are giving various issues:

  1. current_user.image.service_url ---- undefined method `service_url' for #<ActiveStorage::Attached::One:0x....

  2. Rails.application.routes.url_helpers.rails_disk_blob_path(current_user.image, only_path: true), it gives me an output like:

    "/rails/blobs/%23%3CActiveStorage::Attached::One:0x007f991c7b41b8%3E"

but this is not a url, right? I am not able to hit and get image on browser.

  1. url_for ----

    undefined method `active_storage_attachment_url' for #<Api::V1::UsersController:0x007f991c1eaa98

Occlusive answered 19/5, 2018 at 10:27 Comment(2)
Just for more clarification, I want to send image link in json response of api, so that I can display it on mobile appOcclusive
Could you print the full error message of : > 1) current_user.image.service_url ---- undefined method `service_url' for #Instalment
N
15

Use the method rails_blob_path for attachements in a controller and models

For example, if you need to assign a variable (e.g. cover_url) in a controller, first you should include url_helpers and after use method rails_blob_path with some parameters. You can do the same in any model, worker etc.

Complete example below:

class ApplicationController < ActionController::Base

  include Rails.application.routes.url_helpers

  def index
    @event = Event.first
    cover_url = rails_blob_path(@event.cover, disposition: "attachment", only_path: true)
  end

end
Nucleolated answered 8/1, 2019 at 7:29 Comment(2)
rails_blob_path does not accept a variant. I used rails_representation_url(variant_or_blob, disposition: "attachment", only_path: true) with Rails 5.2 and it seems to accept both a regular blob or a variant.Voluptuary
a path is not a url; it's just the part after the/ and omits the protocol, host, and port, so this answer is incorrect -- I'm trying rails_blob_url but that requires some additional configuration I haven't quite figured out yetSfumato
S
9

Sometimes, e.g. an API needs to return the full url with host / protocol for the clients (e.g. mobile phones etc.). In this case, passing the host parameter to all of the rails_blob_url calls is repetitive and not DRY. Even, you might need different settings in dev/test/prod to make it work.

If you are using ActionMailer and have already configuring that host/protocol in the environments/*.rb you can reuse the setting with rails_blob_url or rails_representation_url.

# in your config/environments/*.rb you might be already configuring ActionMailer
config.action_mailer.default_url_options = { host: 'www.my-site.com', protocol: 'https' }

I would recommend just calling the full Rails.application.url_helpers.rails_blob_url instead of dumping at least 50 methods into your model class (depending on your routes.rb), when you only need 2.

class MyModel < ApplicationModel
  has_one_attached :logo

  # linking to a variant full url
  def logo_medium_variant_url
    variant = logo.variant(resize: "1600x200>")   
    Rails.application.routes.url_helpers.rails_representation_url(
      variant, 
      Rails.application.config.action_mailer.default_url_options
    )
   end

  # linking to a original blob full url
  def logo_blob_url
    Rails.application.routes.url_helpers.rails_blob_url(
      logo.blob, 
      Rails.application.config.action_mailer.default_url_options
    )
  end
end

Sadfaced answered 13/3, 2019 at 10:37 Comment(0)
M
0

I didn't have used rails active storage but what i have read in documentation this might help you

Try rails_blob_url(model.image)

For more http://edgeguides.rubyonrails.org/active_storage_overview.html

Mistiemistime answered 19/5, 2018 at 12:26 Comment(4)
This method also turn out to be unavailable.Occlusive
rails_disk_blob_url generates the link, but its a strange link, and also navigating to that link issues following error: (undefined method `verified' for nil:NilClass): activestorage (0.1) lib/active_storage/verified_key_with_expiration.rb:10Occlusive
What i will suggest you is to read the documentation, because there might be some issue with the way you're implementing it, also check if you have image attached to that userMistiemistime
Thanks, Will surely try it on fresh project and get back to this thread if found anythingOcclusive
D
-2

I was able to view the image in the browser using the following:

<%= link_to image_tag(upload.variant(resize: "100x100")), upload %>

Where upload is an attached image.

Demandant answered 21/5, 2018 at 11:8 Comment(1)
I want to use it in the apiOcclusive

© 2022 - 2024 — McMap. All rights reserved.