Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 805
Mobeen Avatar asked May 19 '18 10:05

Mobeen


People also ask

What is active storage in rails?

Active storage is an inbuilt gem in Rails that developers widely use to handle file uploads. Combined with the encrypted credentials feature in the latest releases of Rails, active storage is a safe and easy method to upload, serve, and analyze files onto cloud-based storage services as well as local storage.

What is meant by active storage?

1 What is Active Storage? Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects.


2 Answers

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
like image 189
Jonatas Eduardo Avatar answered Sep 22 '22 14:09

Jonatas Eduardo


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

like image 29
stwienert Avatar answered Sep 22 '22 14:09

stwienert