-
Notifications
You must be signed in to change notification settings - Fork 21.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ActiveStorage with Rails API #32500
Comments
Hi @NicholasLYang I've been using |
Active Storage provides For further information I suggest reading the beta guide for Active Storage. |
Hey @pixeltrix, I appreciate the response, but I'm still a little confused on link generation. For instance, if I'm creating a variant, I get the following error: |
@NicholasLYang this is what is working for me ( I upgraded to Rails 5.2 this morning from RC2 ).
This is how my development.rb looks like
|
@NicholasLYang for a variant you need to use |
@pixeltrix thanks for the work you've put in so far. Anyway you guys could add documentation to the guides for working with AWS? I'm having the same error message when I use ActiveModelSerializers and AWS S3 to host the media files. |
@saurabhbhatia are you hosting your images on the same server? I keep getting the same error message and I think it has to do with using AWS instead of saving the images to the application |
I have the same problem too, When im using # config/development.rb
config.active_storage.service = :local
config.default_url_options = { host: "localhost:3000" }
config.action_mailer.default_url_options = { :host => "localhost:3000" } #config/production.rb
config.active_storage.service = :amazon
config.action_mailer.default_url_options = { host: 'https://kappuchino.herokuapp.com' }
config.default_url_options = { host: "https://kappuchino.herokuapp.com" } And if i am use |
Oh i just fixed the problem, i'm using method |
if you are using fast_jsonapi serializer this may work attribute :cover do |object|
Rails.application.routes.url_helpers.rails_blob_url(object.cover) if object.cover.attached?
end and add this in any initializer |
if you need
|
@madmatvey Thank you so much, it made my day! |
When using the settings suggested by @philiplambok, it fails as follows:
rails: In
What am I missing ? |
Finally the option that worked for me was:
And in my serialiser I used:
The code source of the app is here. |
Thanks for the code @ belgoros , the link is dead. Maybe it is a private repo? |
The following worked the best for me: #concerns/set_current_host_for_disk_service.rb
module Concerns
module SetCurrentHostForDiskService
extend ActiveSupport::Concern
included do
before_action :set_current_host_for_disk_service, if: -> { ActiveStorage::Blob.service.is_a?(ActiveStorage::Service::DiskService) }
end
private
def set_current_host_for_disk_service
ActiveStorage::Current.host = request.base_url
end
end
end #application_controller.rb
class ApplicationController < ActionController::Base
include Concerns::SetCurrentHostForDiskService
...
end P.S. There is a concern on |
@seanfcarroll Oups, my bad, I've merged the branch, everything is on |
One additional question... If my application is multi-tenant and I want to serialize a model with an image in AS, can I use I assume setting |
Hi! I have used this config and it works for me (Rails 5.2.3). It works for # config/routes.rb
Rails.application.routes.draw do
default_url_options protocol: Rails.env.development? ? :http : :https
...
end |
Thats it. thks |
I ended up with following helper.
|
If you want to use url_for with fast_jsonapi serializer And in your serializer: |
When I use Amazon S3 as a storage service in production env, what is the |
Should rails_representation_url(variant) also work when using activestorage in proxy mode? I'm attempting this, but am still getting redirected. |
I'm basically trying to get ActiveStorage working with Rails API. The main issue I have is getting a URL from an Attachment object.
url_for
doesn't work outside of Rails views, so I cannot get the url from that. In addition, I'm not quite clear on creating an article with an image. The following code when run in the Rails console does not create an attachment:School.create(name: "New York University", image: File.open(Rails.root + "db/sample.jpg"))
Instead, it simply gives the following:
Steps to reproduce
Try to run
url_for
in the Rails console.Expected behavior
I get the url. Granted, I get that this is not the purpose of
url_for
(it's a view helper, not a method). But I'd like some equivalent, ideally a method in the ActiveStorage::Attached object.Actual behavior
url_for
gives the following error:(undefined method 'url_for' for main:Object)
. Even when I include the Rails view helpers, I get the following:ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true)
I could try adding a host to the
ActiveStorage::Attached
object, but that's sorta hackneyed as splats don't work with it, and I can't mutate it easily.System configuration
Rails version:
5.2.0.rc2
Ruby version:
ruby 2.5.0p0
Thank you for your time. I understand that ActiveStorage is not fully stable or released yet, but it looked too damn cool to not use 😄 . I'd be open to contributing/opening a PR if someone could point me in the right direction.
The text was updated successfully, but these errors were encountered: