Ruby on Rails Active Storage how to change host for url_for
Today I've Learned postGiven you use Rails 5.2 Active Storage for file uploads
# app/models/account.rb
class Account < ActiveRecord::Base
has_one_attached :avatar
end
# rails console
a = Account.create
a.avatar.attach(io: File.open('/tmp/dog.jpg'), filename: 'dog.jpg')
a.save
by default the url for active storage is set to example.com
# rails console
app.url_for(a.avatar)
# => "http://www.example.com/rails/active_storage/blobs/xxxxxxxxxxxxxxxxxxxxxxxxxx/dog.jpg"
app.rails_blob_url(a.avatar)
# => "http://www.example.com/rails/active_storage/blobs/xxxxxxxxxxxxxxxxxxxxxxxxxx/dog.jpg"
To change the host name:
# config/environments/developent.rb
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
Note: if you need to change http to https change
Rails.application.routes.default_url_options[:protocol] = 'https'
# rails console
app.url_for(a.avatar)
# => "http://localhost:3000/rails/active_storage/blobs/xxxxxxxxxxxxxxxxxxxxxxxxxx/dog.jpg"
app.rails_blob_url(a.avatar)
# => "http://localhost:3000/rails/active_storage/blobs/xxxxxxxxxxxxxxxxxxxxxxxxxx/dog.jpg"
BTW there is also
rails_blob_path
helper to fetch path without host
source:
- http://tech.sasalog.net/2018/05/use-active-storage-with-rails-5.2/
- https://edgeguides.rubyonrails.org/active_storage_overview.html
discussion
Entire blog website and all the articles can be forked from this Github Repo