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

蓝咒 提交于 2020-05-14 18:09:05

问题


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 #

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.

3) url_for ----

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

回答1:


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



回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/50424251/how-can-i-get-url-of-my-attachment-stored-in-active-storage-in-my-rails-controll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!