Set url to be used for assets - ruby-on-rails

I have a rails app
salmonfishing.com
which has a database. I have another app
salmonfishing-test.com
How can I alias the root url so that when a user goes to
http://salmonfishing-test.com
the assets and data from the
http://salmonfishing.com database
will be used.
My database.yml points to the 'salmonfishing' database, but the generated url for an image is
http://salmonfishing-test/bigfish.png
That is as would be expected, however internally I would like that to be aliased to
http://salmonfishing.com/bigfish.png
...basically so all assets and data are served from 'salmonfishing' database, and saved to it. Ran across various ways of setting sub-paths, but not root for assets and data.
Cheers,
Jet

If your only problem is the wrong domain name in your asset URLs, you can change it in your environment config file: config/environments/[whatever].rb:
config.action_controller.asset_host = "http://salmonfishing.com"
This line is already present in production.rb, but it's commented out.

Related

carrierwave file storage saved in public directory but rails does not recognize path

A carrierwave uploader in a Rails 6.1 application defines
storage :file
def store_dir
"archive/#{model.id}"
end
development.rb does define
config.active_storage.service = :local
The file and versions are properly process and saved to the archive directory.
However, when calling
attachment.image_url(:preview).to_s
the link is created, but clicking on it returns a rails routing error:
No route matches [GET] "/archive/3/PHOTO-2021-10-02-17-29-15.jpg"
Calling the path of the image in the browser window returns the same error.
the same occurs with /public/archive[...] and image tags return broken links.
While it is understandably that rails might be oriented towards an image tag expecting the file in the assets/images directory - the application still writes the html tag as
<img src="/archive/3/preview_PHOTO-2021-10-02-17-29-15.jpg" />
and the full URL does not fish out something in the public folder, returning the no route error.
of note the application serves as expected the images when running under localhost. The difference in behaviour is concerning.
two avenues of solution, thus questions:
a) how can Carrierwave be directed to saving in the assets/images directory
b) How can this link be properly generated and served with the full URL from the application's public directory
This question merits an answer as it may be useful for other users.
Carrierwave and ActiveStorage do seem to be able to co-exist together, though I am not certain how as I did not document every step taken (& I wanted to test this hypothesis to a positive conclusion).
A sort of garden path was created via the No route matches error message.
The public directory used by carrierwave was being generated on every release of the application. Thus deployments would get new public directories, but not point to the proper one for items loaded before that deploy. Symlink required.
Solution is to edit deploy.rb, in this case:
append :linked_dirs, 'public/archive'
note: this should be only for the carrierwave defined store_dir

Rails Sprockets file uses local manifest for digests instead of asset host version

In our web application built in Rails we have several clients using the same application who will have different assets that are used dependant on which subdomain is used.
To achieve this we swap out what folder is being used on the CDN like so:
config.action_controller.asset_host = Proc.new { |source, request|
if request.subdomain.present?
"http#{request.ssl? ? 's' : ''}://cdn.domain.com/#{request.subdomain}/"
else
"http#{request.ssl? ? 's' : ''}://#{request.host_with_port}/"
end
}
Each time we create a new client we compile the assets manually using a custom build tool that uses Sprockets to build the assets the same way Rails would and then upload them to our CDN under a folder that matches the subdomain. This then allows us to have different sets of assets based purely on the subdomain.
Now this works fine except that when we update an asset the digest will change for that file but Rails will still try and load the old asset digests because the sprockets-manifest file (which is in /public/assets) e.g. .sprockets-manifest-12345.json is being loaded instead of the one that's on the CDN. Even though the asset host is different it still loads the local one.
Rails it seems doesn't care about other manifest files as the file itself only stores the filename to the fingerprinted version so even when things like the host changes it would normally be able to find the correct asset. It would seem as though Rails has been designed this way deliberately.
However we really need to get Rails to use the manifest file that is on the CDN itself rather than use the one in the public folder local to the application.
After reading the docs, it seems you can change the manifest location. We tried doing it by using the same logic as above for the manifest like so:
config.assets.manifest = Proc.new { |source, request|
if request.subdomain.present?
"http#{request.ssl? ? 's' : ''}://cdn.domain.com/#{request.subdomain}/"
else
"http#{request.ssl? ? 's' : ''}://#{request.host_with_port}/"
end
}
But Rails/Sprockets is still using the local sprockets file... Any ideas why?

Rails asset pipeline doesn't serve images

My Rails app doesn't serve images at all.
image_url('picture.jpg')
# will result in url(http://localhost:3000/images/picture.jpg)
# but should be url(http://localhost:3000/assets/picture.jpg)
image_tag 'picture.jpg'
asset_url 'picture.jpg'
# will result in the same url / path as image_url()
Neither http://localhost:3000/images/picture.jpg nor http://localhost:3000/assets/picture.jpg exists, while http://localhost:3000/assets/images/picture.jpg does.
Here is a gist of my application.rb and development.rb: https://gist.github.com/maximski/1ccb75f6f89c02932239
I am in development environment and I don't want to precompile files manually. The app is pretty much much newly generated so the configuration is almost completely set on default.
This problem appear if images doesn't exists in app/assets/images directory. Check that app/assets/images/picture.jpg file is exists.

Rails 3 : Heroku app using myapp.herokuapp.com as base for URL building

I just deployed one of my apps to heroku. This app uses :
A default "myapp.herokuapp.com" address,
And I got a domain configured so that the app can be reached through "www.myapp.com".
And I noticed today the following issue : my application links are based on "http://myapp.herokuapp.com" domain (hence I get "http://myapp.herokuapp.com/page" URLs) even when I access the app using my domain name (I would then expect to get "www.myapp.com/page" URLs).
I tried to edit my production.rb and set the default_url_options :
# Base domain for url generation
config.action_controller.default_url_options = { :host => "www.myapp.com" }
But it doesn't change a thing. Also tried to change this in application.rb, just in case, but nothing happens either.
Any clue ?
Thanks a lot for your help guys !
Edit : This used to work as expected before today when I did the database migration to the new Heroku postgres thing. Don't know if this can have any impact.
If you're using _path methods for your urls, this is generating a relative path which is always based on the url you visit. If you're using controller/fragment caching, you should probably use _url instead in your views. You might also want to consider setting config.action_controller.perform_caching to false in your production.rb if all your pages have some controller logic.
See this page for more info on how caching works in Rails.
I had a similar problem. It was caused by the following line of code which was pointing to heroku.com and getting redirected to herokuapp.com
config.action_mailer.default_url_options = { :host => 'my-staging-domain.heroku.com' }
I mention it because it's the action_mailer.default_url_options yet clearly it affects the default url options outside of the scope of the mailer if you haven't explicitly set up the action_controller.default_url_options

Rails: non matching asset digests

Below is the asset host config of my Rails app
ASSET_HOSTS = ["http://host1.cdn.com", "http://host2.cdn.com"]
config.asset_host = proc do |path, request=nil, *_|
ASSET_HOSTS.sample
end
The problem with this is that one of my javascript files (which is a .js.erb file) has a different digest (when called by javascript_include_tag) than the one specified in manifest.yml (generated during precompilation).
Does anyone know why this happened and how to fix it?

Resources