14

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

Yuki Inoue
  • 2,652
  • 4
  • 27
  • 40
Mobeen
  • 773
  • 1
  • 6
  • 18
  • Just for more clarification, I want to send image link in json response of api, so that I can display it on mobile app – Mobeen May 19 '18 at 10:32
  • Could you print the full error message of : > 1) current_user.image.service_url ---- undefined method `service_url' for # – Dinatih May 22 '18 at 09:11

4 Answers4

9

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
Jonatas Eduardo
  • 574
  • 6
  • 13
  • 3
    `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. – user2490003 Dec 23 '19 at 07:47
7

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

stwienert
  • 2,249
  • 20
  • 24
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

Ravi Mariya
  • 1,114
  • 15
  • 19
  • This method also turn out to be unavailable. – Mobeen May 19 '18 at 12:31
  • 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:10 – Mobeen May 19 '18 at 12:45
  • 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 user – Ravi Mariya May 19 '18 at 12:55
  • Thanks, Will surely try it on fresh project and get back to this thread if found anything – Mobeen May 20 '18 at 14:48
-1

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.

safikabis
  • 33
  • 6